# Calculating Period or Frequency of a Sinusoidal Signal

13 views (last 30 days)
HM on 6 Mar 2017
Commented: Star Strider on 7 Mar 2017
Hello,
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.
HM on 7 Mar 2017
Great! Thanks for that. I haven't tested it yet, but I see what this function does. It simply takes the product of two consecutive values, and finds the instances where this is negative (you would only get a negative value if the two consecutive values straddle the zero line). I think I should also be able to re-purpose this function to look for crossings of a different value other than zero, by shifting my starting vector up or down so that the crossing value of interest becomes zero.
Thanks again, Hugh.
Star Strider on 7 Mar 2017
My pleasure!
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.