I have a sine waveform that is a result of simulation.
This is always single tone with a constant offset, but with distortion and noise and may have some jitter:
$$s[k] = A\sin\left(2\pi \frac{f}{f_\text{s}} k + \phi\right)+ \alpha+N[k].$$
(since SNR after distortion is better than 40 dB, and jitter probably not dominant, let us ignore distortion and jitter for now.)
I need to create an ideal sine wave that will be the best fit for the given waveform. I am using Cadence calculator functions so, I will explain my current approach as pseudocode.
My input is waveform sampled using coherent sampling - sourceWave
dft1=dft(sourceWave t1 t2 256 "Rectangular") #DFT with 256 points and rectangular window
dc=mag(value(sourceDFT 0)) #Zero frequency DFT result to get sign wave offset
sourceWaveNorm =sourceWave-dc #Remove DC
dft2=dft(sourceWaveNorm t1 t2 256 "Rectangular")#DFT of waveform without DC
sineFreq =maxamplfreq(magnitude(dft2)) #Get frequency with maximum amplitude as it should be frequenxy of the input sine wave
sineAmp=magnitude(value(dft2 sineFreq )) #Amplitude
sinePhase=phaseRad(value(dft2 sineFreq )) #Phase
finalPhase =(sinePhase+ (pi/ 2)) - (sineFreq * 2 * pi * t1)
ideslSineWave=sineAmp*sin(sineFreq*2*pi+finalPhase)+dc
This approach seems to work fine for input waveforms without DC. But when there is DC in the input waveform fitting not good, seems like the DC of ideal waveform is little bit wrong.
Additional clarification: Basically, I need to calculate values for
$$ \hat s(t) = \hat A\sin(2\pi \hat ft + \hat \phi) + \hat\alpha$$
So, I need to estimate $A,f,\phi$ and the offset $\alpha$ from the input waveform.
How can I improve this, or maybe a totally different approach is needed?
sourceWaveNorm =sourceWave-SineOffsis supposed to mean. I'm also not convinced going DFT->subtract->IDFT is an overly clever way to remove a DC component. After all, the 0. DFT bin is literally just the average of all time-domain values; you could have subtracted that without going through the DFT. – Marcus Müller Jun 30 '22 at 09:13$x^2 = \sin(y)$allows you to add formulas to your question's text) define what you want to have as output? – Marcus Müller Jun 30 '22 at 10:52