Generating a Sine Signal (for example a voltage sine wave) with changing frequency and changing sample rate (time)?

i have a measured signal (e.g. voltage sine wave) and i would like to generate a sine wave which matches this meausred signal (with the generated sine wave i remove the harmonics from my measured signal), just like a PLL block in simulink.

for that i have to do the 2 steps:

1- Calculate the frequency of the measured signal from detecting the Zerocrossing.

2- calcualte the sine function using sin(2*pi*f(t)*t).

I have the following problem.

1- calcuating the frequency by counting the samples between two zerocrossing would results in getting the frequency at one point so my calcualted fequency does not match my sampled measured signal.

2- even when interpolating or using the repmat function i am able to get the f(t) to be the same length as the time (t) vector yet my sine wave is not correctly calcualted. i know that:

I am adding the zerocrossing times on my time vector which makes the last one inconsistance which mean the sampling rate is changing also.

so how to genereate a sine wave which changing frequency and changing sampling rate of the time signal?

Best Reagrds

1. If you are doing this in realtime, then your generated signal WILL ALWAYS lag behind the original.
2. Your sampling rate will not change because of anything your calculation does.
3. You have a problem with discontinuities when the frequency of your measured signal changes - you are abruptly changing from the old to the new frequency. This will cause skips in the generated signal.
4. You will not always be able to exactly match the real signal - the zero crossing may take place between two samples. This will cause your calculated frequency to be wrong.
5. The calculated frequency won't necessarily fit well with your sampling rate, so that it won't end exactly on a sample - this will cause another skip in your generated signal.

You will have to gradually change f(t) in order to avoid the abrupt changes. So, you measure f one time and it is (say) 1000Hz, then the next time it is 1100hz. You cannot simply change from one to the other. You will have to "walk" the value for f from 1000Hz to 1100Hz over the length of time it takes for one half cycle of 1100Hz.

That should improve things a good bit.

• thx for the answer. 1- I am not doing this in realtime,2- i am linearly interpulating between my two sampling points to get the exact zerocrossing and adding those points to the measured signal which lead to a different sampling rate, 3- you are correct still using the abruptly changing frequecy has gave me good results before, 4- see point 2, 5- that is why i am already re-destriputing the calculated frequency (with the same values leading to this abrupt changes) to get the same length as the time vector of the measured signal. can you hlep me more? i can send you my code. Aug 14, 2014 at 13:17
• @JRE - although I think I understand the intuition behind the reason for "walking the value for f from 1000 to 1100hz over the length of time it takes for one half of the cycle" it would be awesome to hear it from you (although I know this question is a bit old) Jan 21, 2017 at 1:00