how to generate a Sine wave with changable frequency in mfile?
4 views (last 30 days)
Show older comments
Dear all,
I would like to generate something similler to a PLL sine signal locked on a measured sine signal. for that i have detected the Zeros of my measured, lets say, voltage signal and calculated the freuqncy from counting the number of points between two zero crossing. (the frequency of the measured signal is not constant and changing with time). by using the following equation that was achieved:
f = 1./(2*diff(Zeros));% where Zeros are my detected zerocorssing times in s.
so by knowing the following:
1- I know the frequency of my measured signal (f vector length is not equal to the length of measured signal)
2- I know my sample rate of my measured signal and my time.
3- sin(2*pi*f*t) will generate my locked sine signal to the measured sine wave signal (Voltage)
my question: why am i not getting a correct sine wave signal with the same frequency of the measured signal?
note: Zeros is a vector containing the interpolated time values when my sine wave signal corss the zero value. (so i only have frequency values at the diff(Zeros) values and not at the same sample rate of the measured signal).
Can anyone help me to condition my calculated frequency signal to have the same lenght as the measured signal (and the correct time steps) in order to make the sin(2*pi*f*t) works correctly?
Best Regards
2 Comments
dpb
on 7 Aug 2014
Can't really tell w/o some actual code and a (small) dataset that illustrates the problem, but --
a) if it's not constant frequency your description seems to only have an average over the whole period so it wouldn't ever match any one portion, and
b) zeros is a builtin (and heavily used) Matlab function; use something else for your data array of crossing points to avoid high confusion/unintended behavior.
Accepted Answer
Daniel kiracofe
on 13 Aug 2014
Edited: Daniel kiracofe
on 13 Aug 2014
the short answer is that you cannot just multiply f by t to get what you want. You need to integrate f with respect to time to get theta. i.e. instead of sin(2 * pi * f * t) you want sin(2 * pi * cumtrapz(t, f)).
this short tutorial I wrote goes into more detail: http://mechanicalvibration.com/Generating_signal_from_freq.html
2 Comments
Daniel kiracofe
on 14 Aug 2014
I think the easiest way is to just use interp1(). i.e.
Wtnew = interp1( time_Zeros, W, t); Sine = sin(Wtnew);
in this way, Sine will have a sample at every value of the vector t (which could have any sampling rate you want, even a non-constant one). you could also use cubic spline if linear interpolation is not good enough.
note the mod(Wt, 2*pi) is not necessary. it doesn't hurt, but it also is not doing anything for you.
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering 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!