cftool custom equation doesn't work ?

I have tried to fit the attached data (fit.mat) using custom equation fit, but it does not seem to work.
The equation used is: a1*sin(b1*x+c1) + a2*sin(b2*x+c2) + a3*sin(b3*x+c3) +
a4*sin(b4*x+c4) + a5*sin(b5*x+c5) + a6*sin(b6*x+c6) +
a7*sin(b7*x+c7) + a8*sin(b8*x+c8).
When I tried to use Sum of Sines fit (same data, same equation) it did something.
Is there a problem with Custon Equation fitting?

5 Comments

sin8 has an algorithm for selecting starting points .
on theory sum of sin can be decomposed as an fft . However I was not able to do at all well when I tried to analyse that way.
Could you please show me the code used to compose the signal using fft ?
Could I offer some unsolicited advice? Unsolicited in the sense that it is not pertinent to your direct question, but purely modeling advice that applies to the question I think you really want to solve.
Here is your relationship.
Now, I don't know exactly what you want to get out of this. Whenever you choose to build an empirical model of some sort, you need to know what and why you are building that model.
The problem is I think when you use a model like 'sin8', you are reacting to what looks like a sinusoidal relationship, therefore we must use a sine series to model it. Sorry, but I would disagree. In fact, you don't have a periodic relationship. You have a relationship with a heavily periodic component to it. Even worse, if you look carefully at those waves, they are not actually sine shaped. They look fairly sharp at the peaks, and rounded at the bottom. For example, I'll zoom in on just one of those peaks.
As you see, it is not truly sinusoidal. Fatter and flatter at the min in each period than at the max. If you look at each of those periods, they all seem to have the same fundamental shape.
Regardless, my first inclination here would be to model this relationship as something like:
y = f(time)*sin(a*time + b)
So I would think of this as a product of two relations, one that describes the envelope, and a secondary periodic relation.
In fact, while that will give me something, it would be a moderately poor approximation, because the upper and lower envelopes are not in fact symmetric. If it were symmetrical around the x-axis, we would see a nice smooth upper envelope here:
plot(time,abs(y),'.')
So if I connected the peaks of your curve
Since we dont, I would probably modify my proposed model to be in a general form like:
y = (f(t) - g(t))/2*h(t) + (f(t) + g(t))/2
Here h(t) will be a purely periodic relationship, oscillating essentially from -1 to1. It might be a simple sine wave, or possibly slightly more complex.
f(t) and g(t) there would be functions that define the upper and lower envelopes respectively.
You could essentially get them by using something like findpeaks on your curve, then fitting a pchip spline through the peaks as found, and then do the same on the valleys. (Sorry, but I don't have the signal processing toolbox, so I lack findpeaks.)
How about the periodic part? First, how consistent in width are those periods? I might try finding the locations of the points where your function crosses zero.
pp = spline(time,y);
ycross = slmsolve(pp,0);
hist(diff(ycross),20)
I used my slmsolve tool there, found in my SLM toolbox. It found all points on that curve where it crossed 0.
So it is bi-model, with one peak around 8.5e-8, and another around 1.15e-7. But in fact, that makes some sense, since h(t) is not in fact a nice sine wave, but one with those fat wide bottoms and narrow steep tops.
Essentially though, I think you should be investigating models of the form I describe, rather than a pure sum of sines. The result would be a model that perhaps does offer some utility and some understanding of the process behind this data. Again, I don't really know what you are trying to get out of this. Perhaps a simple interpolating spline fit would be entirely sufficient. But very often, I find people using what I would call the wrong models, possibly because they don't see a better choice asbeing at all possible.
First of all I am sorry if I bothered you with my questions. I insisted because I wanted to get the transfer function from this model.
I apologize again and thank you for your time.
That is a great answer.
Thank you so much.

Sign in to comment.

 Accepted Answer

I think I get the issue. I think it is why does 'sin8' work better than does a custom equation fit? The issue with a custom equation is it can be almost anything. As such, the code will have difficulties coming up with intelligent starting values for the coefficients. The default is a random start. Intelligently provided starting values will go a LONG way. But that sometimes almost requires you already know the answer, or at least something close.
But when you use a canned model like 'sin8', the tool knows something very valuable. At least it does if the programmer was on their toes. Someone who understands modeling might understand that the model has parameters that fall into specific classes. (At least, this is how I would write the code.) It would understand that some of the parameters are essentially linear parameters, that really do not need starting values, nor is iteration on those parameters even needed. That makes the entire problem much simpler.
For example, were I writing the code for fit, when I saw a model like 'sin1', I would recognize the mdoel can be written in the form
y = a*sin(b*x+c)
but that the same model can be written as
y = a1*sin(b*x) + a2*cos(b*x)
AND that I would recognize the parameters a1 and a2 are conditionally linear parameters. So given a value for b, I can then use a simple linear regression to recover a1 and a2. Then by simple mathematics when the fit is done, I can convert the model back into the original form. That is just application of a simple identity for the sine function. I can even put approximate confidence limits around the estimated parameters.
The reason for the expansion that I did was to be able to reduce the problem from one where you need to estimate THREE nonlinear parameters, to a nonlinear estimation with only ONE parameter to search for. As such, it will be MUCH more robust to poorly chosen starting values. It will converge more rapidly.
I'm not sure this is how they wrote the code inside fit. It certainly is the way I would have written that code. Regardless, there are surely other tricks they might have chosen. At least I would have chosen some way to arrive at an intelligent set of starting values, perhaps based on something like the expansion I described.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!