Power Fit — Control Systems/Curve Fitting
Control_Systems/Curve_Fitting/Power_Fit · 1 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Power Fit
Control Systems / Curve Fitting
Fits a power law to the last W samples and emits its two parameters:
ŷ(x) = a·xb
This is MATLAB's power1 library model, and the output carries
a then b – coeffnames(fittype('power1'))'s own
order. a is the fitted value at x = 1 and b is the exponent:
the slope of the curve on log-log axes, so b = 1 is a straight line
through the origin, b = 2 a parabola and b = −1 an
inverse law.
Ports
- u – the sampled signal being fitted. Scalar: one channel and its own window – see Notes.
- ab – the parameter column, [2, 1]: row 1 is a, row 2 is b. Its height is fixed and does not follow any setting.
Parameters
- Window Length – W, how many samples the fit sees. A whole number from 3 to 101. Two parameters need at least two points, and a third is the first that makes it a fit rather than an interpolation. Bounded above because the dot products are unrolled at export.
- Abscissa Start – x₀, where the oldest sample of the window sits on the fitted axis. A single strictly positive number. It has to be positive because xb is not a real number for a negative x and a fractional b, and undefined at zero.
- Abscissa Step – h, the spacing between consecutive samples on that axis, so the newest sample sits at x₀ + (W−1)·h. A single positive number. Set the pair in the same units the power law is written in – seconds of elapsed time, metres of distance – and both parameters come out in those units.
- Ordinate Floor – the smallest ordinate the fit will consider. A single strictly positive number. A sample at or below it is treated as being at it, because a power law is strictly positive in y and a sampled signal is not: the logarithm has no answer at zero or below. Set it just under the smallest value the signal legitimately reaches; a floor far below that lets a single dropout dominate the fit, because in the log domain the distance from 0.001 to 1 is the same as the distance from 1 to 1000.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text.
The 2×W coefficient bank is structural and is inlined into the arithmetic at export time rather than exposed as a tunable parameter: it follows from the window length, the abscissa start and the abscissa step together, and changing any of them changes how many multiplies the core contains. Re-export after changing them. The floor is inlined as a constant beside its own logarithm.
A generated core carries exactly two transcendental calls per step – one ln of the incoming sample and one exp of the fitted intercept. Both are the model itself. The W logarithms of the abscissa, by contrast, never reach a core at all: they were taken once at export time to build the bank.
The three HDL targets are simulation-only real
arithmetic, quantizing only at the port boundary: a logarithm and an exponential
have no Q16.16 form. They additionally clamp the amplitude to the
[0, 32767] that format can carry, which the seven software targets and
this block's own simulation do not.
Simulink bridge
None (Support::None). Fitting a power law to a running
window is a Curve Fitting Toolbox function (fit with the
power1 model), and that toolbox ships no Simulink library at all, so
there is no path a diagram could name. The bridge reports this block rather than
dropping it silently, and it therefore has no parity testbench; code
export verification still covers it across all ten languages.
Notes
- Stateful, and discrete by nature
(
setDiscreteOnlyBlock(true)): the window advances once per sample. - ⚠ The abscissa runs forwards here and backwards on every other block of this family. Polynomial Fit, Fourier Fit, Gaussian Fit and Exponential Fit put the newest sample at x = 0 and the sample i steps back at x = −i·h. A power law has no value at zero and none below it, so this block's window is oldest at x₀ and increases towards the newest sample – an elapsed-time axis rather than a look-back one. Reading it the other way flips the sign of b.
- ⚠ The fit is done in the log-log domain and MATLAB's is not.
ln y = ln a + b·ln x is an ordinary straight-line least squares
whose design matrix is fixed by the configuration – which is what lets the
whole solve happen once and every sample afterwards be two dot products.
fit(x, y, 'power1')instead minimises the residual in y by iteration, weighting large ordinates more heavily, and no closed form produces its answer. The two agree only on data that lies exactly on a power law. Measured on the thirteen-sample window in this block's source banner, the log-log answer is a = 0.8932, b = −0.4355 and R2026a'spower1answers a = 1.0885, b = −0.3819 – 22 % and 12 % apart. If the y-residual objective is what you need, fit offline; this block is the one that runs in a generated core. - ⚠ Only the two-parameter model is offered. MATLAB's
power2, a·xb + c, does not survive the logarithm – an added constant has no linear form in the log domain – so it would need an iterative solve every sample in ten backends. Subtract a known offset with a Bias block first if you have one. - ⚠ The ordinate is floored, and the floor is part of the answer. Every
sample at or below Ordinate Floor enters the fit as that floor, in all ten
targets and in this block's own simulation alike. Flooring is not a convenience:
VHDL's
LOGasserts on a non-positive argument, which aborts a simulation rather than returning a bad number, so it has to exist somewhere and it exists in one place. The abscissa needs no such floor – it is positive by configuration, which is what Abscissa Start is checked for. - One solve, then arithmetic. The 2×2 normal-equation system is solved once when the configuration loads; every sample afterwards is two fixed dot products over the window, plus the one logarithm and the one exponential.
- The window holds logarithms of the ordinate, and is prefilled at y = 1. The register stores ln y rather than y, so the logarithm is taken once per sample rather than W times; every backend zero-initializes its state, and zero in that register means ln y = 0. The first W−1 outputs of a run are therefore a startup transient, as they are on Detrend, Savitzky-Golay Filter, Polynomial Fit, Fourier Fit, Gaussian Fit and Exponential Fit.
- ⚠ Its output is NOT a polynomial, and Evaluate Fit cannot read it.
Evaluate Fit, Fit Derivative and Fit Integral all speak
coefficients in descending powers; these are the two parameters of a power
law, and b is not required to be a whole number. Wiring this block into
any of the three produces a number rather than an error, so the mistake is
silent. To evaluate the fitted curve at some x, compute
a·xb with a Math Function in its
powmode. - For growth in time rather than in a variable, use Exponential Fit. y = a·ebx is the same straight line against x instead of ln x, and Exponential Fit is that block. The two are easy to confuse and are not interchangeable: a power law is straight on log-log axes and an exponential is straight on semi-log ones.
- Scalar only. One channel and its own history; wire one block per channel.
- No state space. Linear in ln y and not in y, so there is no A/B/C/D pair to seed and model reduction correctly declines to merge it.
Code facts#
| Fact | Value |
|---|---|
| registered type | Control_Systems/Curve_Fitting/Power_Fit |
| family | Control_Systems/Curve_Fitting |
| solver environment class | ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Power_Fit |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Curve_Fitting/Power_Fit/ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Power_Fit.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Curve_Fitting/Power_Fit/ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Power_Fit.h |
| default size on canvas | 130 × 72 px |
| ports at insert | 1 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | u |
| 2 | out | ICoreDouble | ab |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Window Length | 12 | — |
Abscissa Start | 1 | — |
Abscissa Step | 1 | — |
Ordinate Floor | 1e-6 | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): fitting a power law to a running window is a Curve Fitting Toolbox function (fit with the power1 model), not a Simulink library block -- that toolbox ships no Simulink library at all -- so there is no path a diagram could name; the block is reported rather than dropped when a model crosses
Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Power Fit -- MATLAB's
power1library model over the last W samples, as the pair (a, b) y(x) = a * x^b
coeffnames(fittype('power1'))in R2026a reports {a b}, and the output column carries them in that order.THE ABSCISSA IS NOT THE FAMILY'S. Polynomial_Fit, Fourier_Fit, Gaussian_Fit and Exponential_Fit put the newest sample at x = 0 and read backwards; x^b has no value at zero and none below it, so this block runs its window on a STRICTLY POSITIVE, INCREASING axis instead: the OLDEST sample sits at x0 ("Abscissa Start") and each newer one is one step h further along, so sample k of the window counting from the oldest sits at x0 + k*h.
THE FIT IS DONE IN THE LOG-LOG DOMAIN. ln y = ln a + b*ln x is a straight-line least squares against ln x, whose design matrix is settled by the configuration, so the normal-equation system is solved ONCE per configuration load and every sample afterwards is two fixed dot products over one shift register.
fit(x, y, 'power1')minimises the residual in y instead, by iteration; the two objectives agree only on data lying exactly on a power law.MEASURED AGAINST R2026a rather than asserted. Over the thirteen-sample window
y = [1.2 0.7 2.5 0.3 0.12 0.8 1.1 -0.22 0.45 1.7 0.35 0.62 0.09] (oldest first)
at W = 13, x0 = 0.6, h = 0.35 and an ordinate floor of 0.28, MATLAB's own straight-line solve of the same points, polyfit(log(x), log(max(y, 0.28)), 1), answers
b = -0.43545541284642364 ln a = -0.11299378342401206 a = 0.8931562124722211
and cond(A'A) there is 9.409. This block reproduces both to 1e-15 relative. On the SAME data MATLAB's nonlinear
fit(x, y, 'power1')answers a = 1.088472963298621, b = -0.38186611981609164 -- 22 % and 12 % away, which is the size of the objective difference on data that is not a power law, and is quoted in the description rather than hidden.⚠ NO LOGARITHM OF A CONFIGURED VALUE SURVIVES INTO A GENERATED CORE: every ln(x_k) was taken once, here, to build the bank. The two transcendental calls a core does carry are one ln() of the incoming SAMPLE and one exp() of the fitted intercept, both of which are the model.
Sample results#
The same rig also ran:
| Stimulus | What it is | Output range |
|---|---|---|
impulse | Impulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair) | 1e-6 … 304.8 |
ramp | Ramp: slope 1 from t = 0 | 1.646e-4 … 6.352 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | 4.224e-9 … 226.9 |
table | Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample | 5.564e-6 … 211.8 |
Plotted: step — Step: 0 -> 1 at t = 1 s
Category dynamic · sample time 0.1 · 60 steps · commit 94aea539e1b63d0ffbcf829426bcd09d1d2a53e5 · produced by docsSample --out <folder> --blocks Exponential_Fit Power_Fit --steps 60 · data docs/generated/samples/Control_Systems__Curve_Fitting__Power_Fit.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).