why square() cannot work with cftool

12 views (last 30 days)
Cheerful
Cheerful on 10 Jul 2012
hi
i have generated a data-series below: x=1:0.01:100; F=square(0.5*x);
then i invoke cftool, under the custom equations, i have the equation: square(b*x)
and run the curve-fitting. It could not fit, and the estimated b always follow the initial value of b. The R-square always negative.
why square() could not used under the custom equations? it should work fine like sine() functions?
thank you

Answers (3)

Tom Lane
Tom Lane on 10 Jul 2012
I suspect this is because the derivative of the function is always 0 or +/-Inf. The optimizer that tries to perturb the inputs in order to find a minimum sum of squared residuals needs a smoother function. For instance, if it perturbed the coefficient a little, it would see no difference in the results:
>> F = square(.5*t);
>> isequal(F,square(.50001*t))
ans =
1
  1 Comment
Cheerful
Cheerful on 10 Jul 2012
Hi Tom
thanks for the very good answer. May I know how to model a square-wave function in order to use min sum of squared residual? I am trying to estimate the period and pulse-width of a data-sets.

Sign in to comment.


Star Strider
Star Strider on 10 Jul 2012
If you want to estimate the period and pulse width of data sets, and the data sets (or a subset of each of them) are square wave pulse trains, I suggest something simple such as ‘diff’:
pulses = diff([0; data]); % For a column vector. The leading ‘0’ preserves the indices so it will match those of ‘data’
Search (use ‘find’) for the positive spikes to mark the beginning of the square waves, then do the same to find the next negative spike to mark the end, to give you the pulse width. The times between positive spikes will give you the period.
This isn't elegant, but if I understand your question correctly, it should give you what you want.
  2 Comments
Cheerful
Cheerful on 10 Jul 2012
yes. that is a very good suggestion. However, actually my real problem is more challenging. Just that I try to break it into simpler parts.
I have a similar data series at the bottom of figure 3. The next step is to estimate the parameters of the inputs at the top of figure 3. That is the period and duty cycle of the sine wave. Hence I try to fit this PWM to the data series using least squares.
thanks alot
Star Strider
Star Strider on 10 Jul 2012
If you have the Signal Processing Toolbox, see if the ‘demod’ function will do what you need. It has a ‘pwm’ option. The only approach I can think of is a ‘for’ loop that calculates each pulse width as it goes by determining the distance between a positive-going pulse and the next negative-going pulse. There must be a better way.

Sign in to comment.


Wayne King
Wayne King on 10 Jul 2012
In R2012, There are a number of new measurement functions in Signal Processing Toolbox. Among them are several applicable to pulse waveforms:
pulsewidth.m, pulsesep.m, pulseperiod.m, and dutycle.m

Categories

Find more on Curve Fitting Toolbox 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!