Generating a Sine Signal (for example a voltage sine wave) with changing frequency and changing sample rate (time)?
If you are doing this in realtime, then your generated signal WILL ALWAYS lag behind the original.
Your sampling rate will not change because of anything your calculation does.
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.
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.
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.