Calculating Period or Frequency of a Sinusoidal Signal
19 views (last 30 days)
Show older comments
HM on 6 Mar 2017
Commented: Star Strider on 7 Mar 2017
I'm trying to write some code to calculate the period (and hence frequency) of a decaying sinusoidal signal. I'm specifically interested in calculating the times that the signal crosses a particular value. For the purpose of this example, let's assume that value is zero.
I have the signal data in a vector, with each sample being 0.01 seconds apart. My intention was to use interp1() function to return the time stamp of each time the signal crosses my value of interest, e.g. the zero value. I was hoping interp1() would just would return a vector of ALL the crossings, that way I could access whichever ones I was interested in. Then I could just take the time difference between two consecutive 'zero crossings' and that would be half the period. What I'm finding is that the interp1() function is not returning a meaningful value, presumably because the signal crosses the zero line multiple times.
It's most likely that I'm using the completely wrong function to do this, but it's all I can think of at the moment (being relatively new to matlab). I don't have the signal processing toolbox, so can't use the FFT function.
Any other suggestions would be much appreciated. Thanks, Hugh.
Star Strider on 6 Mar 2017
Edited: Star Strider on 6 Mar 2017
You can find the indices of the zero-crossings of your signal with this little utility function:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
You can use those indices in a for loop with interp1 to return the ‘exact’ x-values of the zero crossings. Note that there may be a ‘wrap-around’ effect at the end, so you may want to discard the last index.
If you want to fit a sinusoidal function to your data, How to filter noise from time-frequency data and find natural frequency of a cantilever? will offer guidance, including the calculation of the period.
EDIT — Added caution about the end-effect of my ‘zci’ function.
Star Strider on 7 Mar 2017
You absolutely understand the ‘zci’ function.
To get the zero-crossings, subtract the median (or mean, the median may be more accurate) from your signal, find the period, then add the median value back to your original signal.
More Answers (0)
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!