2

Question:- I have to calculate the FFT of time series data.

Time series data of what ? I have a Vibration sensor that is giving me the data of displacement in x and y coordinate.

What are the Problems? I can't find a proper library to work although I found some but I don't know if it is working fine or not. te As far as I know, we need Sample Rate and FFTsize(number of elements) to calculate the FFT

I used the library https://www.npmjs.com/package/fft-js in which I passed a 1D array and it returns a 2D array having some positive values and some negative values.

Question I have

  1. How does it calculate FFT without the sampling rate as far as I know a sampling rate is needed
  2. For calculating FFT does data frequency need to be constant as like idealy I am having 170 data per second but sometime if goes down as 165 and 160
  3. What are ways I can verify my fft

Ps:- I am trying to plot fft in realtime

help or resource are much appreciated as I Can't find any resource of fft in javascript

WOLFIE
  • 29
  • 2

1 Answers1

2

Checking the fft-js implementation I see that it is using a basic DIT (Decimation in time) radix-2 algorithm. Also, looking at their implementations of complex arithmetic, I see it is using a length-2 array to represent the complex numbers.

FFT is agnostic to sample rate, but the interpretation of the results depends on the sample rate. The FFT bin at position k has frequency min(k * N, N - k*N) * Fs / N, where Fs is the sampling frequency.

FFT assumes constant frequency within the block, if the sampling rate varies but the sampling period is the same for all the samples that's fine, you just change the Fs in the formula. If the sampling rate is changing randomly then you will add some noise to your estimates. If the variation is because you cannot process as many samples as you would like is preferable to reduce the sample frequency to ensure the samples come at a constant rate.

Suppose you choose to use a fft of size 2**7 = 128, then your frequencies will be freq(k) = min(k / Fs, (128 - k) / 128 ant the energy for the frequency k will be given by X[k][0]**2 + X[k][1]**2 + X[N-k][0]**2 + X[N-k][1]**2, you will probably want to plot this energy for k=1:63 in a logarithmic scale.

If you want to separate vibration in the axes x and y, you do two FFT with the component of the vibration in each direction separately.

If you are in the browser you can use an analyzer node, intended for audio analysis, but does what you need. In our case you will need to put your data in a audio buffer source node then connect to the analyser.

A few online demos (with audio, but the analysis is the same for your vibration)

https://p5js.org/examples/sound-frequency-spectrum.html

F sound F sound

SH sound SH sound

My favorite one https://musiclab.chromeexperiments.com/Spectrogram/ You can see on the bottom of the page a few pre recorded samples, you can use your microphone, or you can use the mouse to draw something that will be played.

enter image description here

Bob
  • 13,867
  • 1
  • 5
  • 27