C:\> Rostislav Persion's Projects

.:: Spring Mass Fourier Transform ::.
Fourier Transform using Spring Mass System






This is a fourier transform demo based on the concept of tuning forks and sping mass systems. A spring and a mass have a resonance frequency. By feeding an analog signal to a set of springs and masses you will see some of them resonate. By measuring their average velocity, you can determine the frequencies in a signal.



SOFTWARE WRITTEN IN C#
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Threading;
   6:   
   7:  namespace ConsoleApplication1
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              while (true)
  14:              {
  15:                  int n = 10;
  16:                  double[] k = new double[] {1,8,15,22,29,36,43,50,57,64};
  17:                  double[] m = new double[] {1,1,1,1,1,1,1,1,1,1};
  18:                  double[] x = new double[n];
  19:                  double[] vx = new double[n];
  20:                  double[] ax = new double[n];
  21:                  double dt = .1;
  22:                  double t = 0;
  23:   
  24:                  double[] av = new double[n];
  25:                  int avcounter = 0;
  26:   
  27:                  for (int i = 0; i < n; i++)
  28:                  {
  29:                      x[i] = 0;
  30:                      vx[i] = 0;
  31:                      ax[i] = 0;
  32:                  }
  33:   
  34:                  //clear average
  35:                  for (int i = 0; i < n; i++)
  36:                  {
  37:                      av[i] = 0;
  38:                  }
  39:   
  40:                  //get sine wave frequency
  41:                  Console.Write("Frequency (1 to 100): ");
  42:                  double sinfreq = Convert.ToDouble(Console.ReadLine());
  43:   
  44:                  while (t < 10)
  45:                  {
  46:                      for (int i = 0; i < n; i++)
  47:                      {
  48:                          ax[i] = (forcefunction(t,sinfreq) + (-x[i] * k[i])) / m[i];
  49:                          vx[i] += 2 * ax[i] * dt;
  50:                          x[i] += 2 * vx[i] * dt;
  51:                      }
  52:                      t += dt;
  53:   
  54:                      //count averages
  55:                      avcounter++;
  56:                      for (int i = 0; i < n; i++)
  57:                      {
  58:                          av[i] += Math.Abs(vx[i]);
  59:                      }
  60:   
  61:                      //draw
  62:                      Console.Clear();
  63:                      DisplayMass(x[0], m[0], k[0]);
  64:                      DisplayMass(x[1], m[1], k[1]);
  65:                      DisplayMass(x[2], m[2], k[2]);
  66:                      DisplayMass(x[3], m[3], k[3]);
  67:                      DisplayMass(x[4], m[4], k[4]);
  68:                      DisplayMass(x[5], m[5], k[5]);
  69:                      DisplayMass(x[6], m[6], k[6]);
  70:                      DisplayMass(x[7], m[7], k[7]);
  71:                      DisplayMass(x[8], m[8], k[8]);
  72:                      DisplayMass(x[9], m[9], k[9]);
  73:                      Console.WriteLine();
  74:                      DisplayMass(forcefunction(t,sinfreq), 0, 0);
  75:                      Thread.Sleep(50);
  76:                  }
  77:   
  78:                  //count averages
  79:                  Console.WriteLine();
  80:                  for (int i = 0; i < n; i++)
  81:                  {
  82:                      av[i] /= avcounter;
  83:                      Console.WriteLine(av[i] + " m/s");
  84:                  }
  85:                  Console.WriteLine("Loops: " + avcounter.ToString());
  86:              }
  87:          }
  88:   
  89:          public static double forcefunction(double t, double f)
  90:          {
  91:              return (Math.Sin(t / (2 * Math.PI) * f));
  92:          }
  93:          public static void DisplayMass(double x,double m, double k)
  94:          {
  95:              string buff = "freq: " + String.Format("{0:0.00}", Math.Sqrt(k / m)) + " mass: " + m.ToString() + " k: " + k.ToString() + " [";
  96:              int num = (int)Math.Floor(10 * x)+10;
  97:              for (int i = 0; i < num; i++)
  98:              {
  99:                  buff += "X";
 100:              }
 101:              buff += "X";
 102:              Console.WriteLine(buff);
 103:          }
 104:      }
 105:  }