Need help/ideas solving a problem
Show older comments
Hey all,
I have a problem I would like to solve, but am not even sure how to go about solving it. I have taken real motion data of sine wave form, but obviously the sine wave is not perfect. What I want to do is decompose this measured data so that I can extract a value for the variables (amplitude, phase, etc.) that make up this distorted sine wave. Then I want to find the error value of these variables from the ideal sine wave that I want. I have looked at some regression stuff, but I am not sure how I can use that for my application. So ideally, I would like to take some data and be able to tell exactly how much amplitude, phase error there is. Simple calculating this is not an option as the error coupld be caused by all error sources or combination of errors.
Answers (3)
Geoff
on 1 May 2012
You can have a go at using simple function minimisation: best fit
Start with a vector that represents all your shape characteristics: (amplitude, frequency, phase, offset)
x0 = [1, 10, 0, 0];
Make a function that generates a sine wave over a bunch of time values using those characteristics:
waveFn = @(x,t) x(1) * sin(t * 2*pi*x(2) - x(3)) + x(4);
Now you need to make that function into an objective function for minimisation. That is, a sum of square errors. Let's assume you have your motion values in the vector data and the corresponding time values in t. For this example, I'm just going to invent t.
t = 1:length(data);
objFn = @(x) sum( (data - waveFn(x,t)) .^ 2 );
Then you take a 'reasonable' initial guess, which can be quite unreasonable if you like because this is a simple sine-wave with presumably an obvious primary frequency... And you put that into fminsearch() - the bare-bones swiss army knife function solver.
options = optimset( 'MaxFunEvals', 10000, 'MaxIter', 500 );
x = fminsearch( objFn, x0, options );
Out pops your solved wave parameters, and you can generate the wave to test how good it looks. Hopefully it looks good.
dataFit = waveFn( x, t );
plot( [data(:) dataFit(:)] );
legend( 'Original', 'Fitted' );
You can play with your objective function. Declare a real function instead of an anonymous lazy-function, and you can do whatever you like to compare the data with the fit... You might want to do calculations on variance, or maximum error, or any number of things. Whatever number you spit out, remember that smaller means better.
Hope this is a help, and is actually relevant to what you are trying to achieve. =)
4 Comments
Geoff
on 1 May 2012
A corollary to this is that you can model the kinds of imperfections you expect to see. For example, you may choose a small handful of numbers to represent time distortion - in effect, correcting for frequency variations. The same goes for amplitude, etc. Bottom line is that you can only detect 'noise' in data if your underlying model is simpler than the noise.
Wes
on 10 May 2012
Wes
on 10 May 2012
Image Analyst
on 10 May 2012
You know what really helps explain it better? A picture. Can you upload an image, screenshot, plot, whatever, to tinypic.com?
Image Analyst
on 1 May 2012
0 votes
I think it's best to avoid the noise in the first place. Not sure how you're acquiring the signal but you may want to consider a lock-in amplifier ( http://en.wikipedia.org/wiki/Lock_in_amplifier) which was invented for precisely this situation.
If you're stuck with the bad signal, you might look at the FFT or PSD (power spectral density) to get those parameters. They're built for that kind of thing, while a regression like polyfit() is not.
3 Comments
Wes
on 1 May 2012
Geoff
on 1 May 2012
Perhaps you could post a plot of some of this data, or explain how 'perfect' it is supposed to be. Is it 2-dimensional, or 3-dimensional? If your wave characteristics keep changing over time, then you could determine how many samples make up a single wave cycle (or however many cycles should be expected to be constant), and piece-wise solve your curve in overlapping sections (much the same concept as moving averages), smooth the results, and you'll have a matrix that contains the full evolution of your waveform characteristics from the beginning to the end of your data.
Richard Brown
on 1 May 2012
What does the distortion look like? (have you got a picture?) A least squares fit may or may not be appropriate depending on your noise ...
Wes
on 10 May 2012
0 votes
3 Comments
Geoff
on 10 May 2012
Oh, in that case I would start with the suggestions I made in my answer. This has become a model fitting problem, and that is the "least squares minimisation" you have been told to do. Whether you use fminsearch() or the more robust fsolve() is up to you (and your toolbox licensing). You have already parameterised your model into a function that generates the kind of shapes that you measure... You just need to find out what parameters will generate the best data.
So what you do is define your wave function (as you probably have already) to accept all the possible control values. Then you vectorise that. If one of your parameters is a 2D position, for example, it becomes two values in your parameter vector. Now you have EVERYTHING that you need to generate a wave to match the data (including an initial time value and duration).
You run the minimiser over this, computing the sum of square residuals (your measured data minus your predicted data for a given vector). It does the rest. What pops out is the best-fit parameter vector. And that is the answer you are looking for.
I did this same kind of thing for umpire reviews in international cricket. I had a bunch of semi-noisy data points and I needed to compute a complete equation of flight. There were about 27 different parameters, even including gravity and air pressure which both vary with altitude and geographical location... If the sum of squares method doesn't satisfy, you can create a better objective function that is tuned to your problem.... But you should start with that, because I think it will solve your problem.
Hope this helps.
Wes
on 23 May 2012
Geoff
on 24 May 2012
Great news! Glad you got it sorted =) I'd appreciate if you accepted my answer to close this question off correctly. If you need any more input on this feel free to send me a message through my user profile or just post another question to the community.
Categories
Find more on Simulation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!