1

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?

Marcus Müller
  • 30,525
  • 4
  • 34
  • 58
Alex
  • 271
  • 1
  • 2
  • 6
  • not quite sure what sourceWaveNorm =sourceWave-SineOffs is 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
  • @Marcus Müller sourceWave-SineOff is mistale. It is sourceWave-dc removing dc. You probably right, because I don't sure i did it right i came here and asking for help :) – Alex Jun 30 '22 at 10:42
  • ok, I'd love to recommend something, but the problem here is that your requirements aren't really clear. Unless your sine wave has a frequency of 0 Hz, there's no DC in a sine wave. So, could you maybe more precisely (if possible, with mathematical formula (inline LaTeX, e.g., $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
  • @Marcus Müller The output I want is a sine wave that is the best fit of the input sine wave. So if the input sine has offset (DC) the output sine wave should have the same offset. Basically, I need to calculate values for A * sin(2 * pi * f+phi)+offset. I need to calculate A,f,phi,offset from the input wave – Alex Jun 30 '22 at 11:22
  • ah, ok, yeah, that makes sense :) So it's not only the sine wave! – Marcus Müller Jun 30 '22 at 11:40
  • @MarcusMüller Thanks for adding nice formula to the question post :) – Alex Jun 30 '22 at 11:47
  • no problem! So, let's now come up with an equally nice formula for the input including its imperfections! – Marcus Müller Jun 30 '22 at 11:53
  • I just went ahead and added an input signal model. It's basically the same as the output signal model, but incorporates additive noise. Can you check whether that looks like it represents the signal model you have in mind well? What I've not done is model 1. the jitter: Should we be modelling that as phase noise? Or somehow different? (Might really not be easy to answer!) and 2. the distortion. Should I be putting an exponent, say, $\gamma$ to your sine, i.e. $s(t) = \sin(\cdots)^\gamma + \ldots, \quad \gamma \ne 1$? – Marcus Müller Jun 30 '22 at 11:58
  • @MarcusMüller Thank you. Probably exact input model is not very important as I want generic solution. In my case input signal may come from oscillator or just sine wave distorted by non-perfect amplifier or non-perfect sampling, so it is difficult to say what the exact model will be. – Alex Jun 30 '22 at 12:18
  • it is important! If you say your model has variability, that's totally OK, but a good estimator needs to know what they're up against. So, ideally, we put that variations as random variables into that formula. – Marcus Müller Jun 30 '22 at 12:22
  • For example: your distortion, which I read as "nonlinear effect", will add new tones to the signal that aren't in there. So, if I wrote an estimator that depends on there being exactly one harmonic signal in the mixture, it would fail. And: "non-perfect amplifier with non-perfect samplig" is not that hard to model, actually. Don't give up estimator performance because you think the modelling's going to be hard. I promise it's not that hard! – Marcus Müller Jun 30 '22 at 12:26
  • I'll start with trying to get a feeling for the orders of magnitudes of different effects. So, what's the range of frequencies $f$ you're interested in, and what's the sampling rate? We're now in a discrete regime, so my signal model formula isn't actually right, by the way, need to fix that. – Marcus Müller Jun 30 '22 at 12:28
  • @MarcusMüller So basically, noise, jitter, and nonlinear effects are added to the signal. Jitter is a less frequent case, so I can do without it if it is too complicated. The input waveform is always "sampled" because there are no continuous signals in simulation :) f is usually in the order of GHz and approximately known (with accuracy of 10%, let's say) In many cases I know the frequency exactly. – Alex Jun 30 '22 at 14:01
  • agreed, but small problem: nonlinearity is not just added to the signal; it modifies it (see my proposed $\lambda$ exponent!); so, let's ignore jitter for now. How nonlinear are your amplifiers, worst case? – Marcus Müller Jun 30 '22 at 14:24
  • (you're not quite right about simulation always happening in discrete time; you can simulate the equations for continuous-time of such signals just as well, depending on what you need to figured out (for example, if you had a 3D model of a quartz crystal, and tried to figure out how it oscillates, the result might very well be coefficients for a continuous-time oscillator model!)) – Marcus Müller Jun 30 '22 at 14:24
  • @MarcusMüller Usually SNR better than 40dB – Alex Jun 30 '22 at 14:45
  • aha! So we THD < 40 dB, necessarily. That's pretty nice to know! – Marcus Müller Jun 30 '22 at 14:55

1 Answers1

2

The standard way to do this would be to:

  1. Remove / estimate the DC offset ($\hat{\alpha}$)
  2. Estimate the frequency $\hat{f}$.
  3. Change the polar estimation problem for $\hat{A}$ and $\hat{\phi}$ into a rectangular form by using $$\sin(X + Y) = \sin X \cos Y + \cos X \sin Y$$ and estimating $\hat{A}\cos(\hat{\phi})$ and $\hat{A}\sin(\hat{\phi})$.

Item 1. can just be done using: $$ \hat{\alpha} = \frac{1}{N} \sum_{n=0}^{N-1} s[n] $$

Item 2. can be done using the Quinn-Fernandes frequency estimator. There is a Python implementation here.

Item 3. can be done using the formulae from Kay that I transcribed here.

Peter K.
  • 25,714
  • 9
  • 46
  • 91
  • 1
    by the way, the $\hat \alpha$ given here (the average!) is exactly the 0.th DFT bin, just calculated without the rest of the DFT :) – Marcus Müller Jun 30 '22 at 14:57
  • @MarcusMüller Good point! It's possible to try to do 1. and 2. using just the FFT. 1. will be as you say. 2. might not work so well because of the discretization of the estimated frequency. – Peter K. Jun 30 '22 at 14:58
  • exactly; if you need to have fine-resolution frequency deteciton of a single tone, the DFT is probably a pretty bad choice. Using a parametric estimator (like your QNF, which I hadn't heard of before!) makes much more sense. – Marcus Müller Jun 30 '22 at 15:00
  • 1
    (I just mentioned the DFT because OP's original algorithm calculates the DFT, and then throws away all but the 0.th bin to get the DC value :) ) – Marcus Müller Jun 30 '22 at 15:06
  • So for a reason not clear to me, average works better than 0th FFT. Regarding Quinn-Fernandes frequency estimator. I need to write it in a different language, so I will try to reverse engineer the python script. I looked for an explanation of this method but didn't find any public one. – Alex Jun 30 '22 at 17:26
  • @Alex Send me an email to kootsoop at gmail dot com and I can forward you a PDF of the original paper. – Peter K. Jun 30 '22 at 17:53