matlab-review-fi-object-code
Reviews MATLAB fixed-point (fi) code for performance, code generation efficiency, and correctness. Identifies antipatterns and suggests idiomatic improvements. Use when reviewing fi, fimath, numerictype, or quantizenumeric code.
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-review-fi-object-code && cp -r /tmp/matlab-review-fi-object-code/skills-catalog/code-generation/matlab-review-fi-object-code ~/.claude/skills/matlab-review-fi-object-codeSKILL.md
# fi Best Practices Review
Reviews MATLAB code for fixed-point (`fi`) best practices and suggests improvements for performance, code generation efficiency, and correctness.
## When to Use
- Reviewing MATLAB code that uses `fi`, `fimath`, `numerictype`, or `quantizenumeric`
- Optimizing fixed-point simulation speed
- Preparing fixed-point code for C or hardware code generation
## When Not to Use
- Code using only built-in integer types (`int8`, `uint16`, etc.) without `fi`
- Pure floating-point algorithms with no fixed-point intent
- Simulink-only workflows where fixed-point is configured through block dialogs (use Fixed-Point Tool instead)
## Checklist
When reviewing code, check for ALL of the following:
### 1. Vectorize fi() Calls
**Problem**: Scalar `fi()` in a loop is slow due to per-element object construction overhead.
**Fix**: Pass entire arrays to `fi()` at once.
```matlab
% BAD — slow: per-element fi object construction
for k = 1:N
x_fi(k) = fi(x(k), 1, 18, 16, F);
end
% GOOD — fast: single vectorized call, bit-true identical result
x_fi = fi(x, 1, 18, 16, F);
```
### 2. Separate Data Types from Algorithm
**Problem**: Hardcoding fi types inside algorithm code makes it impossible to switch between float/fixed or compare configurations.
**Fix**: Use a types table with empty prototypes and `cast(...,'like',...)`.
```matlab
% Types table (separate function)
function T = mytypes(dt)
switch dt
case 'double'
T.b = double([]); T.x = double([]); T.y = double([]);
case 'single'
T.b = single([]); T.x = single([]); T.y = single([]);
case 'fixed16'
F = fimath('RoundingMethod','Floor','OverflowAction','Wrap', ...
'ProductMode','KeepLSB','ProductWordLength',32, ...
'SumMode','KeepLSB','SumWordLength',32);
T.b = fi([], 1, 16, 15, F);
T.x = fi([], 1, 16, 15, F);
T.y = fi([], 1, 16, 14, F);
end
end
% Algorithm — no hardcoded types
function [y,z] = myfilter(b, x, z, T)
y = zeros(size(x), 'like', T.y);
for n = 1:length(x)
z(:) = [x(n); z(1:end-1)];
y(n) = b * z;
end
end
% Entrypoint — wraps types + cast + algorithm
function [y,z] = entrypoint(dt, b, x)
T = mytypes(dt);
b = cast(b, 'like', T.b);
x = cast(x, 'like', T.x);
z = zeros(size(b'), 'like', T.x);
[y,z] = myfilter(b, x, z, T);
end
```
**Validation**: Run with `'double'` first, then `'single'` (catches single-precision issues early — important for embedded targets where double is unavailable or slow), then `'fixed16'`.
### 3. Prevent Bit Growth with Subscripted Assignment
**Problem**: `acc = acc + x(n)` overwrites `acc` with a new fi object whose type may change due to FullPrecision word growth.
**Fix**: Use `acc(:) = acc + x(n)` to retain the original data type.
```matlab
% BAD — acc type may grow each iteration
acc = fi(0, 1, 32, 16);
for n = 1:numel(x)
acc = acc + x(n);
end
% GOOD — preserves acc's declared type
acc = fi(0, 1, 32, 16);
for n = 1:numel(x)
acc(:) = acc + x(n);
end
```
### 4. Configure fimath for Your Target
**Problem**: Default fimath (Nearest rounding, Saturate overflow, FullPrecision) generates bloated code. A simple `a + b` can produce many lines of C with sign-extension and overflow checks.
**Fix**: Choose fimath settings based on your code generation target.
```matlab
% For C targets (MATLAB Coder) — models integer truncation behavior
F_c = fimath('RoundingMethod','Floor', 'OverflowAction','Wrap', ...
'ProductMode','KeepLSB', 'ProductWordLength',32, ...
'SumMode','KeepLSB', 'SumWordLength',32);
% For DSP processor targets — models shift-right behavior
F_dsp = fimath('RoundingMethod','Floor', 'OverflowAction','Wrap', ...
'ProductMode','KeepMSB', 'ProductWordLength',32, ...
'SumMode','KeepMSB', 'SumWordLength',32);
% For FPGA/hardware targets — use the built-in helper
% hdlfimath = Floor/Wrap/FullPrecision (hardware coder manages bit widths internally)
F_hw = hdlfimath;
x_fi = fi(x, 1, 18, 16, F_hw);
```
**Product/Sum mode selection**:
| Mode | Behavior | Use when |
|------|----------|----------|
| `KeepLSB` | Keep least significant bits (C integer truncation) | Targeting C/C++ (MATLAB Coder) |
| `KeepMSB` | Keep most significant bits (shift-right) | Targeting DSP processors |
| `FullPrecision` | Retain all bits (word growth) | Hardware coder (manages widths internally), or debugging |
| `SpecifyPrecision` | Manual word/fraction lengths | Custom precision requirements |
**Note**: `hdlfimath` returns Floor/Wrap/FullPrecision. The hardware coder manages bit widths through its own pipeline — do not use `KeepLSB` or `KeepMSB` with it unless explicitly required by your design constraints.
**Rounding efficiency** (most to least efficient for codegen):
1. `Floor` — two's complement truncation, no extra logic
2. `Zero` — truncation toward zero
3. `Nearest` — ties to +inf (default)
4. `Convergent` — ties to nearest even
5. `Round` — ties away from zero (most expensive)
**Overflow**: `Wrap` (no logic) vs `Saturate` (requires comparison).
**Slope-bias scaling**: If your fi objects use slope-bias (non-power-of-two slope or non-zero bias):
- `ProductMode` and `SumMode` must be `'SpecifyPrecision'` with `CastBeforeSum` set to `true`
- Hardware code generation and DSP System Toolbox do not support slope-bias — use binary-point for hardware targets
- Slope-bias maximizes accuracy per bit when values are bunched away from zero (e.g., sensor ranges like 273–283 K)
- Match net scaling so operations resolve to shifts; non-zero bias makes multiplication costlier, but zero-bias with non-power-of-two slope can still produce shift-only code
### 5. Preallocate fi Arrays
**Problem**: Growing fi arrays inside loops causes quadratic memory and time growth.
**Fix**: Preallocate using `zeros(...,'like',...)` with a prototype.
```matlab
T = fi([], 1, 18, 16, F); % empty prototype
Y = zeros(N, 1, 'like', T); % preallocated out>
Analyze AMS waveform data using Mixed-Signal Blockset utilities: phase noise measurement, clock jitter, anti-aliased resampling, timing measurements, lock time, INL/DNL, ADC/DAC calibration, HSpice import. Use when analyzing time-domain voltage from PLL/VCO/clock simulations, measuring phase noise from variable-step solver output, computing jitter, or resampling non-uniform data.
Analyze data using MATLAB. Use when the task involves tables, timetables, time-series data, numeric arrays, sensor matrices, or gridded data — including but not limited to exploring, filtering, sorting, cleaning, transforming, aggregating, smoothing, padding, trimming, and answering questions about data. MATLAB provides extensive, easy-to-use built-in functions for these workflows with no additional products required.
Analyze the effective toolbox file set to produce a Dependency Manifest — classify all transitive dependencies as included, product, add-on, or external-unresolved, then present resolution options with tradeoffs. Use after matlab-define-toolbox-api when the spec is approved.
S-parameters, insertion loss, fields, currents, mesh control, and solver selection for RF PCB performance validation. TRIGGER: user asks to compute S-parameters, analyze insertion/return loss, extract fields or currents, compare MoM vs FEM, or control mesh for any RF PCB component. Invoke BEFORE writing sparameters() or solver code — API is non-obvious. SKIP: designing or creating components (use the specific matlab-design-pcb-* skill), material/stackup setup only (use matlab-manage-pcb-material), optimization sweeps (use matlab-optimize-pcb-design), PDN/IR-drop analysis (use matlab-analyze-pcb-pdn).
Analyze antennas installed on electrically large conducting platforms using MATLAB Antenna Toolbox. Loads platform geometry from STL/STEP/IGES, installs antenna elements, selects electromagnetic solvers (MoM-PO, FMM, MoM), and computes patterns, impedance, coupling, and efficiency. Use when the user wants to model an antenna on a vehicle, aircraft, ship, satellite, or other large structure.
PDN DC voltage/current analysis, IR drop, design rule checking, and multi-net batch analysis on imported PCB layouts. TRIGGER: user asks about power integrity, PDN analysis, IR drop, voltage distribution, current density, power nets, or design rule checking on a PCB. Invoke BEFORE writing code — the PDN API chain is specialized and non-obvious. SKIP: importing a PCB file (use matlab-read-pcb-layout), EM field/S-parameter extraction (use matlab-analyze-em), material/stackup setup only (use matlab-manage-pcb-material), transmission line design (use matlab-design-pcb-txline).
Calculate and visualize monostatic and bistatic radar cross section (RCS) using MATLAB Antenna Toolbox. Computes RCS of platforms, antennas, and arrays with PO, MoM, and FMM solvers, supporting HH/VV/HV/VH polarization, GPU acceleration, and near-field observation. Use when the user wants to compute, plot, or analyze radar cross section.