Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: How to fit a sine wave to undersampled data
Posted:
Jan 7, 2013 9:38 PM


"Scott Rauscher" wrote in message <kcelj6$s4t$1@newscl01ah.mathworks.com>... > "Greg Heath" <heath@alumni.brown.edu> wrote in message <kbiqn8$d78$1@newscl01ah.mathworks.com>... > > "Scott Rauscher" wrote in message <kbg0a3$afm$1@newscl01ah.mathworks.com>... > > > I've poured through the forums several times and have almost found a good solution. > > > > > > I want to fit a sine wave to some undersampled constantfrequency/phase/amplitude data. I have hundreds of data files so I need to automate it. I have used SineFit in the file exchange, and it works well most of the time, but has a tendency not to converge. I have also tried sinfapm, but the phase it gives never lines up. > > > > > > What I'm trying to do is best illustrated in this picture (red is the data, blue is the sine wave I want to find): http://s2.postimage.org/98qvgbnu1/matcode.jpg > > > > > > Does anyone have suggestions on where to start for a fullproof way of doing this? > > > > Not enough info. > > > > Do you get a dominant frequency with the dft ? If so, you can estimate A and B > > from the linear problem > > > > xi = A*cos(w*ti) + B*sin(w*ti); % w and ti known > > > > Hope this helps. > > > > Greg > > Thanks for the response Greg. I'm been feeling under the weather, but let me elaborate with some code: > > x = data; > N = length(x); > n = 2:N1; > xs = x(n1)+x(n+1); > C = xs'*x(n)/(x(n)'*x(n))/2; > frequency = acos(C)*SF*.5/pi; %Gives a good estimate > > % xi = A*cos(w*ti) + B*sin(w*ti); % w and ti known > i1 = 2; > i2 = 15; > x1 = TX_i(i1); > x2 = TX_i(i2); > c1 = cos(2*pi*frequency*t_section(i1)); > c2 = cos(2*pi*frequency*t_section(i2)); > s1 = sin(2*pi*frequency*t_section(i1)); > s2 = sin(2*pi*frequency*t_section(i2)); > B = (x2  (x1*c2/c1))/(s2  (c2*s1/c1)); > A = (x1  B*s1)/c1; > t = linspace(t_section(i1),t_section(i2),100); > fit = A*cos(2*pi*frequency*t + pi) + B*sin(2*pi*frequency*t + pi); > plot(t_section,TX_i(astart:aend),'r',t,fit,'b') > > output: http://s1.postimage.org/6yqy4ikun/untitled.jpg > > So that method works well (thank you), but I'm having the same problem as before in that the phase shift is still slightly off. I suppose this is a complication of limited resolution/sampling frequency, but since the shift is visible I should be able to mathematically figure it out, right? > > Any suggestions?
1. Use all (not just 2) of your data points to estimate w via a dft.
2. Use all of yor data points to estimate A and B from a least squares backslash solution.
Hope this helps.
Greg



