Log in

View Full Version : measure amplitude of a sine


jmac698
30th March 2011, 04:16
Hi,
I'm trying to write some analog video calibration software.
I need to measure the amplitude of a sinewave.
Your first thought might be to measure the peaks. This will never work. The true peak is most likely in between two samples, thus you have to interpolate. Considering I have to deal with noise, even guessing where the peak is is going to be hard. The peak is quite flat around that area, and noise could make it appear anywhere.
There is also the problem of potential clipping.
What I need is a way to measure average amplitude over a whole cycle.
The next idea is to divide the samples by a reference sinewave. This doesn't work too well, anyhow I would normalize the measured wave by finding the average (say, 126 in rec601 levels), then subract the average from each sample to bias it around 0. Now I have the problem of division by 0 or small numbers which will be incredibly inaccurate. I would get fancy by doing a scaled average where the greater the absolute different between the reference and measured, the more weight given to the average. I haven't figured out the right way to scale this. (maybe do it in log space?)
I could add the two sinewaves and measure the new phase, working backwards with an identiy I could get the amplitude. This seems promising to me.
a*sin x+b*cos x=sqrt{a^2+b^2}*sin(x+phi)
phi=sin-1(b/(a^2+b^2)
where a=1 and I'm trying to find b. I have a way to measure phi. (How would I solve something like 1.3=b/(1+b^2) ?)

Another way is to find the MSE between a reference and a measure and use the amplitude of the reference for the answer. Is this valid though, because of the scaling involved? It would give much more emphasis to the larger values.
Any ideas?
thanks

ps maybe I could measure the average of each half cycle and work backwards from the ideal area under the curve formula. Anyone know the area of a half cycle?

IanB
30th March 2011, 08:45
RMS=Sqrt(Sum(a[i]**2)/N), for a pure sine wave, Peak = Sqrt(2)*RMS

jmac698
30th March 2011, 18:12
angle sin sin^2
0 0 0
30 .5 .25
60 sqr(3)/2 .75
90 1 1
120 sqr(3)/2 .75
150 .5 .25
180 0 0
210 -.5 -25
240 -sqr(3)/2 .75
270 -1 1
300 -sqr(3)/2 .75
330 -.5 .25


Sum is 6, out of 12 samples, average is .5, rms=sqr(.5)=.707, peak=sqr(2)*sqr(.5)=1
Thanks

Gavino
30th March 2011, 21:41
Sum is 6, out of 12 samples, average is .5, rms=sqr(.5)=.707, peak=sqr(2)*sqr(.5)=1
Since sin^2(x) = (1 - cos(2x))/2 = 1/2 - cos(2x)/2, it's obvious that it's average over a cycle is 1/2.

jmac698
31st March 2011, 06:30
It works fairly well. I made the values +-109 (sin()*109) and the amplitude was accurate to within .3. With also noise of +25 (even distribution), it was still within 1. With clipping (error=-5 in one point), it was just a bit less (-.8). The biggest issue is missing some samples, thus not measuring a full cycle. Missing just one sample, it was error=-10 (measured amplitude was 100 when it should be 110). For one example, at the greatest slope, the difference between successive points was +-55. If I miss this sample it makes a huge difference to the calculation. Thus measuring 11 of 12 points, with a certain phase, can change the sums quite a bit.
So why would I miss a sample? I assure I'm not doing it intentionally; I'm measuring a sinewave which appears at a random point on a line. If I don't make a good estimate of where the sine starts, I may not end up with all the samples. The missing samples will be black. But remember there is noise present as well, so I can't precisely find anything.
Well, to solve that I guess I could make just a bit longer sinewave and find my way to somewhere inside it.

So there's one more problem. The measurement is going through a non-linear scaling. For example if the input is 120,121,122,123,124,125 the output is 120,122,124,124,124,125.
Ok, I tested this and it's on the order of the clipping error. I also need a measure of confidence.
So I'm guess I'm ok :)
thanks