# How to calculate frequency of a periodic signal?

I know there are similar topics but I can not find any solution in my situation. I have an accelerometer sensor and a basic microcontroller. Accelerometer data is collected in an array via i2C. I want to evaluate collected data within 700 mseconds windows. For example, there is a raw signal below:

Then I apply median filter to remove notches:(I think this is good for decreasing peak counts in next process)

Then I found the peak values:

I thought that I can evaluate the peak values for calculating frequency. Is there a way to find frequency ? Or Can I use these peak values to find frequency ? I need a minimal algorithms, because it will be calculated in a microcontroller.

• the peak values are not really a useful reduction of your data. Autocorrelation would yield that, or conversely (i.e. identical in information) the Fourier transform of your data. Apr 3, 2022 at 11:26

You should go straight into a Fourier Transform, and detect your peaks in frequency, to recover your Dominant Frequency.

If you wish, comment in here, and put some data available online in here for more.

Thank you for your replies. I tried fft on octave.

There is a series of 680 samples same with in my first post. But when I try to change sample count on same example, the frequency is changing. I know there is a relationship between them, but I can not understand. How can I scale these sample counts?

Here there is 3 example in order below,

[d680, f=2,49Hz] [d512, f=7,21Hz] [d256, f=1,94Hz]

d680, f=2,49Hz

d512, f=7,21Hz

d256, f=1,94Hz

• 1.- You have 1ms per sample, so you should have 1/2*1/1ms=+-500 Hz maximum frequency instead of 100 Hz, since you must discard half of the data by symmetry. So you should have 341 points at 500 Hz max. 2.- You should take the maximum peak as your dominant frequency (should be close to 13Hz: your answer). The rest, are present of course, but not the dominant frequency. Apr 9, 2022 at 17:49
• Calculated frequency vector is like this: Fs = (1/0.005); #5ms sample time, fvec = (-Nfft/2:Nfft/2-1)*Fs/Nfft; After that I discard half of the data by symmetry and select the dominant frequency. But frequency change is so different, I can not accept this result. Apr 10, 2022 at 8:37
• Again. Your sample time should be 1ms, so your maximum frequency should be 1/2/1ms=500 Hz. Or you do not have 680 samples. Or your maximum time is not 680 ms. The spectrums seems to be ok, and the dominant also seems to be close to 13Hz (under the proper scale), so the error is coming from you. Check these figures, your time plots, and your real sampling time, and come back. Apr 11, 2022 at 4:41
• OK. I rearranged some things. There is no problem now. Also, I tried in C to find fft series. Octave and C results are same with eachother. But I have another problem: When I move the C codes into embedded side, I can not compile because of lack of floating point features. Is there any library which has small footprint and at basic level ? Apr 17, 2022 at 14:11