Prev: SVM
Next: How do I remove the loop in this?
From: Frank on 3 Mar 2010 05:07 "Matt" <mjacobson.removethis(a)xorantech.com> wrote in message <ghp3p7$1d5$1(a)fred.mathworks.com>... > "Matt" <mjacobson.removethis(a)xorantech.com> wrote in message <ghp1cc$ibi$1(a)fred.mathworks.com>... > > "Jeff Klein" <jklein(a)physics.umn.edu> wrote in message <ghouei$1u8$1(a)fred.mathworks.com>... > > > I wonder if anyone has any advice on fitting on a sine wave in MATLAB. > > > > > > The situation is that I have a sine-wave time stream that is from the position encoder of a rotating mechanism in an experimental setup. I want to know, with great accuracy, the frequency of this sine wave. I want to measure this frequency every 5 or 10 cycles of the wave (so obviously a Fourier transform would not yield a high enough resolution). > > > > > > The problem is that, when I try to fit to a sine wave, the fit is highly dependent on the starting value I choose for the frequency parameter. My understanding is that if I vary this parameter slightly (say by 5% or so), the solution should be the same as the solution I got *before* I varied it - but it's not. Because of this, I obviously cannot trust this result. The fitting algorithm also often does a pretty bad job of finding the right amplitude (often visually off by up to 50%), but seems to think that it's converged anyways. > > > > > > How can I make the MATLAB fit function do a better job, and converge on the same result even when I vary my starting parameters a little bit? > > %%%%%%%%%%%%%%%%%%%%%%%% > > > > I'll call your given sine wave data vector X. > > Could you try something like this? > > > > X=X(:)'; %ensure data is a row vector > > > > ddX=diff(X(:),2,2); %Take numerical 2nd derivative > > > > freq=sqrt(X\ddX); %frequency estimate > > > > > > Success would would depend, I guess, on how noisy your data vector X is. > > > > %%%%%%%%%%%%%%%%%%% > > I've refined my idea somewhat, making it more noise-robust by integrating instead of differentiating: > > freq=2; %ground truth frequency > t=linspace(0,pi,1000)'; > dt=diff(t(1:2)); > > X=sin(freq*t); %generate simulated sine wave > X=X+randn(size(X))*.05; %Add some noise > Y=cumsum(cumsum(X))*dt^2; %integrate the data twice > > params=[X,t,ones(length(X),1)]\Y; %Least squares parameter fit > > >>freqEst=sqrt(-1/params(1)), %Estimated frequency > > freqEst = > > 2.0156 > > > Good enough? Hi everybody! I have found this old discussion and found it very interesting. In fact, it would be perfect to me if somebody could tell me how to deal with the same situation but with a period of the modulating function (ideally a monochromatic sine wave) that has some error on the period at each step. That is to say, instead of having noise only on the data, having noise also on the "dt" vector as defined above. In this case, in fact, it seems that the fit is reproducibly mistaking, depending on the initial phase (the value of the first point of the X vector to fit). Thanks in advance
|
Pages: 1 Prev: SVM Next: How do I remove the loop in this? |