Matlab Prepare Signal Data
MathWorksOfficialCreated by MathWorks
- Skill
- Tool integration
- Data and AI
- BSD-2-Clause
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to signal data preparation, guiding users through the lifecycle of cleaning raw signals, managing datastores, applying labels, and partitioning data for machine learning. It emphasizes using high-level Signal Processing Toolbox functions to ensure readability and maintainability, while offering clear heuristics for ordering conditioning steps like detrending, smoothing, and outlier removal. The skill is designed to help users move from raw data to a ready-to-train state for models like trainnet.
What it does
- Cleans signals by filling gaps, removing drift, denoising, and handling outliers.
- Synchronizes and resamples multi-channel data onto a uniform time base.
- Builds data pipelines using signalDatastore for efficient loading.
- Creates labeledSignalSet objects for structured labeling and Signal Labeler interoperability.
- Performs stratified splits and framing for long signal sequences.
- Shapes and prepares datastore outputs for machine learning workflows.
When to use it
- When you need to clean a single signal before analysis or training.
- When preparing a dataset of signals for machine learning with specific labeling requirements.
- When you need to align multiple signal channels or resample them to a uniform rate.
- When you need to partition signal data into training, validation, and test sets.
- When you need to frame long signals for windowed analysis or supervision.
Advantages
- Promotes readable, maintainable code by preferring high-level toolbox functions.
- Provides a logical, verified order for conditioning steps to prevent data corruption.
- Reduces manual effort by automating common tasks like label derivation from filenames.
- Ensures compatibility with Signal Labeler and deep learning training functions.
- Offers clear guidance on when to escalate to lower-level methods.
Usage examples
- “How do I remove a linear trend from my signal?”
- The agent will recommend using detrend(x, 1) and explain why explicit degree specification is preferred for code clarity.
- “I have a folder of signal files; how do I create a datastore and split them for training?”
- The agent will guide you through using signalDatastore to load the files, followed by splitlabels to perform a stratified split.
- “My signal has gaps; how should I fix them?”
- The agent will suggest using fillmissing for short gaps or fillgaps for longer gaps in oscillatory signals, depending on the signal characteristics.
- “How do I align two signals with different sampling rates?”
- The agent will recommend using the synchronize function to place both signals onto a shared time grid.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/matlab/matlab-prepare-signal-data/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip matlab-prepare-signal-data.zip -d .agents/skills
Documentation by MathWorks
Prepare Signal Data
Look in Signal Processing Toolbox first. The conditioning, labeling, splitting, framing, and partitioning helpers here live in Signal Processing Toolbox — not in Stats & ML Toolbox or generic-MATLAB string utilities.
The arc: condition a raw signal (clean it) -> load a folder into a
datastore -> label -> split / frame -> hand off to trainnet. Each
stage is a workflow file; this page routes you to the right one.
When to Use
Cleaning a single signal before analysis: fill gaps, remove drift, deoutlier, denoise, put it on a uniform time base, align multiple channels.
Loading / preparing signal data for ML training: datastores, labels from filenames or folders, stratified splits, framing, parallel processing.
Structured labeling:
labeledSignalSetfor Signal Labeler, all label types.
When NOT to Use
Raw
.wavaudio classification with Audio Toolbox available.audioDatastoreis the canonical path (this skill's custom-ReadFcnworkflow handles.wavonly when Audio Toolbox is absent — references/wf-custom-readfcn.md).Frequency-selective filter DESIGN (band isolation, notch, custom FIR/IIR) — see the
matlab-design-digital-filterskill. This skill's conditioning is about cleaning, not designing filters.Computing per-frame features (RMS, crest factor, spectral / bandwidth, time-frequency features) from an already-conditioned signal — see the
matlab-extract-signal-featuresskill. This skill'sframesig/framelblare for manual per-window labeling / supervision, not for deriving a feature table; thesignal*FeatureExtractorobjects window internally and emit the table.
Best practices
Deliverable is a runnable
.mscript the user can save, version, and re-run — not workspace state.Prefer the highest-level function that does the job.
detrend/smoothdata/fillmissing/resampleread cleanly and are easy for a non-expert to follow. Drop to a lower-level / more-configurable path (designfilt+filtfilt, a hand-built AR model, a named primitive) only when you need control the high-level call cannot give, or when the user asks. Readability first; escalate to low-level for necessity, not by default.The high-level call usually exposes the control you think you need. In particular
smoothdata(x, "sgolay", fl)takes the frame lengthflas an argument — it does NOT hide it — so prefer it over callingsgolayfiltdirectly. Reach forsgolayfiltonly for what the dispatcher genuinely lacks (derivative output viadn, or an unusual polynomial order).
0. Common reflexes
If your first instinct is one of these, the canonical replacement is one row away.
| Reflex | Canonical | Detail |
|---|---|---|
Hand-design a highpass/designfilt to remove a smooth drift | detrend(x, n) — escalate n = 1 -> 2 -> 3 before reaching for a filter; polynomial detrend has unity passband gain | references/fn-detrend.md |
Invent a gap-filler (regularizeNaNs, inpaintn — not real) | fillmissing (interp) for short gaps; fillgaps (SPT, AR) for long gaps in oscillatory signals | references/wf-repair-missing.md |
Hand-roll retime + shift + retime + concat to align channels | synchronize(A, B, ...) — one call to a shared grid | references/wf-align-channels.md |
Custom ReadFcn for a .csv | signalDatastore default reader + SignalVariableNames | references/fn-signaldatastore.md |
cvpartition for a datastore split | splitlabels + subset(ds, idx{k}) | references/fn-splitlabels.md |
regexp / extractBefore / fileparts for labels from filenames | filenames2labels(sds, Extract=...) | references/fn-filenames2labels.md |
regexp / nested fileparts for labels from subfolders | folders2labels(sds.Files) | references/fn-folders2labels.md |
Manual framing loop with (i-1)*hop+1 | framesig(x, fl, OverlapLength=...) | references/wf-frame-and-label.md |
Manual ROI-to-frame vote with containers.Map | framelbl(rois, ...) | references/wf-frame-and-label.md |
for loop load(file) to read in-file label variables | signalDatastore(folder, SignalVariableNames=["x","label"]) | references/fn-signaldatastore.md |
signalMask when you need Signal Labeler interop | labeledSignalSet with ROI labels (signalMask can't import) | references/fn-labeledsignalset.md |
signalLabeler(lss) (pass the set as an arg) | Launch bare signalLabeler (zero args), then Import -> From Workspace or From File | references/wf-label-and-export.md |
SPT-specialized functions exist — reach for them, don't reinvent.
fillgaps(AR gap fill),medfilt1/hampel(impulse handling),sgolayfilt/smoothdata(...,"sgolay")(feature-preserving smoothing) are in Signal Processing Toolbox.
1. Workflows
Each workflow file is the entry point and lists the functions it uses. Start here.
| Workflow | Use when | Reference |
|---|---|---|
| Repair missing samples | NaN gaps / dropouts to fill. | references/wf-repair-missing.md |
| Detrend, smooth, deoutlier | Drift, spikes, and/or broadband noise on one signal (smoothing/denoising lives here). | references/wf-detrend-smooth-deoutlier.md |
| Align multi-rate / offset channels | Several channels onto a shared time base. | references/wf-align-channels.md |
| Put one channel on a uniform rate | One channel -> uniform grid at a chosen rate: jittery timestamps to regularize, OR already uniform but the wrong rate to resample. | references/wf-uniform-rate.md |
| Wavelet denoising (escalation) | Non-stationary/multi-scale noise a tuned sgolayfilt can't remove; wdenoise (Wavelet TB). | references/wf-denoise.md |
| Envelope extraction | Amplitude outline (AM demod, peak hull) — not cleaning. | references/wf-envelope.md |
| Load + label + split | Folder of files -> datastore for training. | references/wf-load-and-split.md |
| Frame long signals + per-frame labels | Long signals, per-window supervision. | references/wf-frame-and-label.md |
| Label + export (all label types) | Structured labels (attribute/ROI/point/TF-ROI), export to Signal Labeler / DL. | references/wf-label-and-export.md |
| Parallel processing across a parpool | Per-signal work across workers. | references/wf-parallel-process.md |
| Custom ReadFcn (only when needed) | Format isn't .mat / .csv, or has a metadata prelude. | references/wf-custom-readfcn.md |
Hand-off to trainnet | Datastore ready; shape for trainnet / combine. | references/wf-handoff-to-dl.md |
Each workflow file names the
fn-reference pages for the functions it uses; there is no separate function index — enter through the workflow that matches your task, or the reflex table above.
2. Ordering when a signal needs several conditioning steps
The governing principle (this is the real rule): order the steps so an
earlier operation does not corrupt the input to a later one. Spikes bias
least-squares fits and get smeared by filters/resamplers; an un-removed trend
gets averaged into the signal by a smoother; most operations choke on NaN.
Reason from that for the signal in front of you — do not follow a fixed chain
blindly.
Default heuristic (a good starting order, not a universal law):
outliers -> detrend -> smooth, with fill and align placed by the principle above.
outliers -> detrend -> smooth is the verified core: remove spikes before a polynomial
detrend(a spike biases the fit) and before a smoother (a smoother spreads the spike across its window); detrend before smooth so the smoother isn't averaging across a trend.Fill
NaNbefore any step that can't handle missing data (detrend, filters, most smoothers).Align / resample: putting a signal on a new grid (
retime/synchronize) createsNaNat non-overlapping times, so fill after aligning. BUT if the signal has spikes, deoutlier before resampling —resample's anti-alias filter will smear an un-removed spike. So align-vs-outliers order depends on the signal; the principle decides, not a fixed sequence.
Not every signal needs every step — identify which apply, order them by the principle, and each workflow file has an off-ramp if your problem is actually a different family.
Copyright 2026 The MathWorks, Inc.
Files
- ATTRIBUTION.mdAdded by AutoGineer1.0 KiB
- LICENSELicence file added by AutoGineer1.4 KiB
- SKILL.md9.5 KiB
- manifest.yaml650 B
- references/fn-countlabels.md2.1 KiB
- references/fn-detrend.md4.6 KiB
- references/fn-filenames2labels.md4.8 KiB
- references/fn-fillgaps.md3.3 KiB
- references/fn-fillmissing.md4.2 KiB
- references/fn-filloutliers.md5.0 KiB
- references/fn-folders2labels.md3.4 KiB
- references/fn-framelbl.md7.1 KiB
- references/fn-framesig.md4.1 KiB
- references/fn-hampel.md3.3 KiB
- references/fn-labeledsignalset.md9.1 KiB
- references/fn-labelspectrogramoptions.md4.6 KiB
- references/fn-medfilt1.md3.3 KiB
- references/fn-partition.md4.6 KiB
- references/fn-resample.md4.6 KiB
- references/fn-retime.md4.7 KiB
- references/fn-sgolayfilt.md3.5 KiB
- references/fn-signaldatastore.md11.9 KiB
- references/fn-signallabeldefinition.md5.9 KiB
- references/fn-signalmask-getmask.md8.6 KiB
- references/fn-smoothdata.md4.8 KiB
- references/fn-splitlabels.md5.3 KiB
- references/fn-subset.md3.9 KiB
- references/fn-synchronize.md4.7 KiB
- references/fn-wdenoise.md5.2 KiB
- references/wf-align-channels.md6.6 KiB
- references/wf-custom-readfcn.md5.0 KiB
- references/wf-denoise.md2.6 KiB
- references/wf-detrend-smooth-deoutlier.md6.6 KiB
- references/wf-envelope.md5.3 KiB
- references/wf-frame-and-label.md4.7 KiB
- references/wf-handoff-to-dl.md9.1 KiB
- references/wf-label-and-export.md8.7 KiB
- references/wf-load-and-split.md4.6 KiB
- references/wf-parallel-process.md4.8 KiB
- references/wf-repair-missing.md6.7 KiB
- references/wf-uniform-rate.md4.2 KiB
references/wf-handoff-to-dl.md
Workflow: hand-off to trainnet
Functions used:
transform,combine,signalDatastore,arrayDatastore,trainnet
The signal-side pipeline is done — datastore is ready, labels are aligned,
shape is per-observation. This file covers the last mile: shaping the
datastore output so trainnet accepts it, and the per-observation layout
each input-layer family expects.
The trainnet datastore contract
trainnet requires the datastore to return cell arrays or tables with
numInputs+numOutputs columns — predictors first, targets last
(see trainnet doc, "Datastore" section under each input-data type).
For the common single-input / single-output supervised case, that is a
1×2 cell {predictor, response} per read.
Two canonical signal-side ways to land in that shape:
Path A — single signalDatastore with two variables
When predictor and response live in the same MAT file (e.g.
spec, label):
sds = signalDatastore(folder, IncludeSubfolders=true, ...
SignalVariableNames=["spec","label"], ReadOutputOrientation="row");
% read(sds) -> 1x2 cell: {spec, label}
trainedNet = trainnet(sds, layers, "crossentropy", options);Path B — two datastores joined with combine
When predictor and response live in separate datastores (signals on
disk; labels derived via filenames2labels / folders2labels):
sigDs = signalDatastore(sigFolder, FileExtensions=".mat", ...
SignalVariableNames="signal");
labels = filenames2labels(sigDs, Extract="Class" + lettersPattern(1));
sigDsT = transform(sigDs, @(p) {zscoreSig(p)}); % cell-wrap is load-bearing
labelDs = arrayDatastore(labels); % default OutputType="cell"
cds = combine(sigDsT, labelDs);
% read(cds) -> 1x2 cell: {signal, label}
trainedNet = trainnet(cds, layers, "crossentropy", options);combine produces a CombinedDatastore whose per-read output is built
by horzcating the underlying reads. For that horzcat to produce the
1×N cell trainnet consumes, every underlying datastore must emit
its value wrapped in a 1×1 cell. The two halves of that contract:
Label side:
arrayDatastore(labels)defaults toOutputType="cell"— this is what makes the label come back as a1×1cell, not a bare scalar. Don't override toOutputType="same"; you'll strip the cell wrap andhorzcatwill try to concatenate a numeric signal with a categorical label —Unable to concatenate a double array and a categorical array.Signal side:
transform(sigDs, @(p) {fn(p)})— the{...}around the function output is the cell wrap. Without it the read is a bare array andhorzcatagain fails with the same type-mismatch.
Per-observation layout trainnet expects
trainnet interprets each cell-element via the input layer's data
format. The layout your datastore must emit per observation depends on
the input layer family. The shapes below are from the trainnet doc
(predictor tables under images / sequences / features).
| Input layer | Per-observation predictor shape | Format | Notes |
|---|---|---|---|
featureInputLayer(c) | 1×c row (or c×1 col, with InputDataFormats="CB") | BC | Tabular / per-window features. |
sequenceInputLayer(c) | s×c (time × channel) | — | s = time steps, c = channels. Datastore reads dim-1 as time, dim-2 as channel — single-channel 1024-sample signal is 1024×1, not 1×1024. Wrong orientation triggers Layer 'sequenceinput': Invalid size of channel dimension. |
imageInputLayer([h w c]) | h×w×c | SSC (per obs) → SSCB batched | h, w, c = height, width, channels. |
image3dInputLayer([h w d c]) | h×w×d×c | SSSC (per obs) → SSSCB batched | 3-D images. |
Older
trainNetwork-era cell-array code often usesc×s(channel × time). That's not what a datastore read should emit. Both moderntrainnetcell arrays and datastore reads ares×c(time × channel). If your code path was originally written for the legacy cell-array form, drop the transpose when you migrate to a datastore.
If the read shape is right but trainnet still complains, the next
axis to check is the data format (InputDataFormats training option,
or a formatted dlarray); see the trainnet reference under
"Input Arguments" → the matching predictor section.
End-to-end Path B (runnable, 1-D classifier)
A folder of .mat files, each with one signal variable (1024 samples,
single channel), classes encoded in the filename prefix
(ClassA_001.mat, ClassB_001.mat, ...), trained against a sequence
input layer.
folder = "data/classify";
% 1. datastore + labels
sds = signalDatastore(folder, FileExtensions=".mat", ...
SignalVariableNames="signal");
labels = filenames2labels(sds, Extract="Class" + lettersPattern(1));
% 2. transform: z-score, keep 1024x1 (time-by-channel), wrap in 1x1 cell
sdsT = transform(sds, @(p) {zscoreSig(p)});
% 3. combine signal + label datastores -> {predictor, response} per read
labelDs = arrayDatastore(labels); % default OutputType="cell"
cds = combine(sdsT, labelDs);
% 4. small 1-D CNN
layers = [
sequenceInputLayer(1, MinLength=1024) % c=1; reads are 1024x1 (s×c)
convolution1dLayer(16, 16, Padding="same")
reluLayer
globalAveragePooling1dLayer
fullyConnectedLayer(3)
softmaxLayer
];
opts = trainingOptions("adam", ...
MaxEpochs=1, MiniBatchSize=8, Shuffle="every-epoch", ...
Verbose=true, Plots="none");
net = trainnet(cds, layers, "crossentropy", opts);
function out = zscoreSig(p)
if iscell(p), p = p{1}; end
x = p(:); % force column -> 1024x1 (s×c)
out = (x - mean(x)) / max(std(x), eps);
endThree load-bearing details that are easy to miss:
p(:)returns a column. That's1024×1—s×cfor the sequence layer. A row vector here triggers the channel-dimension error.@(p) {zscoreSig(p)}cell-wraps the transform output. The{ }around the call is the wrap; without it,combinecan't produce a1×2cell.arrayDatastore(labels)defaultOutputType="cell"is load-bearing. Don't passOutputType="same".
Wrapping a transform into the cell-pair shape
If you transform a multi-variable datastore (path A, two variables in
each MAT file), unpack with c{1} / c{2} and re-wrap:
sdsT = transform(sds, @(c) {preprocess(c{1}), c{2}});
% per-read: 1x2 cell {predictor, response}The cell-unpack idiom (c{1}, c{2}) is the same one used in
wf-frame-and-label.md step 4 for joint signal+label framing.
Per-observation string→categorical conversion in a transform must pin the category set:
categorical(str, cats)wherecats = categories(labels)is read once upstream. Plaincategorical(str)produces 1-element category sets per read;trainnetthen aborts at the validation peek.
If you only have a single-variable predictor (no label in the
datastore), transform(sds, @(p){preprocess(p)}) is a predictor-only
wrap — fine for inference, not sufficient for supervised training,
which still needs the response column from somewhere (path A or B).
OutputDataType / OutputEnvironment (R2024b+)
Two signalDatastore properties that affect the data you hand off,
without writing a transform:
OutputDataType—"same"(default) /"double"/"single", or a per-variable string array. Casts each read so it matches the precision your network expects. Prefer this over a castingtransform.OutputEnvironment—"cpu"(default) /"gpu". With"gpu", numeric reads are returned asgpuArray, sotrainnetconsumes them directly. Requires Parallel Computing Toolbox.
Don't confuse
signalDatastore.OutputDataType(precision cast) witharrayDatastore.OutputType(wrap mode). They sound alike and they both have a"same"value, but they do different things:OutputDataType="same"keeps the stored numeric class;OutputType="same"onarrayDatastorestrips the cell wrap. For thecombine→trainnetpattern,arrayDatastore's default"cell"is what you want — overriding to"same"breaks the contract.
When trainnet still complains after the read shape is right
The issue has moved past data ingestion. Check InputDataFormats (or a
formatted dlarray) against the matching predictor section in the
trainnet doc.
Decision boundary — when transform() is enough
If transform(sds, @fn) returns the per-observation cell-pair shape
above, trainnet(sdsT, layers, lossFn, opts) is the next call.
If you need:
Custom batching (e.g. grouping by sequence length) →
minibatchqueue.GPU prefetching → either set
OutputEnvironment="gpu"on thesignalDatastore, or useminibatchqueuewithOutputEnvironment="gpu".Per-batch transformations →
minibatchqueuewith aMiniBatchFcn.
See also
wf-load-and-split.md — produces the datastore that feeds in here.
wf-frame-and-label.md — joint framing recipe; step 4 shows the same
c{1}/c{2}cell-unpack idiom.fn-signaldatastore.md —
OutputDataType/OutputEnvironmentfull surface and SignalVariableNames variations.
Copyright 2026 The MathWorks, Inc.
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- MathWorks
- Publisher
- MathWorks (Official)
- Source
- matlab/matlab-agentic-toolkit · skills-catalog/signal-processing/matlab-prepare-signal-data · commit 2a7f215
- Licence
- BSD 2-Clause License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE.md)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by MathWorks or MathWorks.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-prepare-signal-data.zip