Need help/ideas solving a problem

2 次查看(过去 30 天)
Wes
Wes 2012-5-1
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.

回答(3 个)

Geoff
Geoff 2012-5-1
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 个评论
Wes
Wes 2012-5-10
I should also add that I am looking at this data outside of the time domain. Its plotted from 0-2pi vs. amplitude and x vs. y in the 2 dimensional plot.
Image Analyst
Image Analyst 2012-5-10
You know what really helps explain it better? A picture. Can you upload an image, screenshot, plot, whatever, to tinypic.com?

请先登录,再进行评论。


Image Analyst
Image Analyst 2012-5-1
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 个评论
Geoff
Geoff 2012-5-1
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
Richard Brown 2012-5-1
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
Wes 2012-5-10
Hey, sorry I have not responded. I was pushed onto solving a different problem and so I got sidetracked. Let me better explain what I am doing so maybe you guys have a better understanding of what I am trying to do.
So I am taking 2 dimensional motion data (very low noise) of a commanded sine wave. The machine does not react perfectly to this command and instead of moving back and forth along the intended line, it actually ends up giving me a square/circular motion when plotting in the x vs. y domain. So I have created a matlab code that will simulate a sine wave with the errors (phase,amplitude, and some if/then statements that describe/define other errors). What I want to do is find out what combination of these errors run through my code will result in the "best fit" to what we are measuring. In other words, trying to quantify the error values based on how i have defined them in the m file simulation.
I was told the best way to do this is a least squares optimization method and so I was wondering how to do this? I was getting a little confused from the Matlab Help so I was wondering if someone could help explain how to that and if that is the best way of optimizing for this situation? Any code help is appreciated as this is not my strongest suite.
  3 个评论
Wes
Wes 2012-5-23
Thanks Geoff for your help. Really appreciated it. I think I figured it out, everything seems to be working as hoped. I started over completely. I imported data, set global variables and known parameters, then created the function loop with the initial guess and fminsearch.....In the ffunction I defined the unknown parameters and used those in the equations to create the waves and solve for the distance differences. Passed the output back to the main program and plotted. Seems like its working well :) My main problem was understanding the structure of fminsearch and creating my own function, but once I got that worked out....All is well :) Thanks again.
Geoff
Geoff 2012-5-24
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by