why square() cannot work with cftool
12 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
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
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
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.
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
0 Comments
See Also
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!