stftLayer error: "Size of input in T dimension must be greater than or equal to window length" despite correct input shape

Hi everyone,
I'm encountering a confusing issue when using the stftLayer in MATLAB's Deep Learning Toolbox. I'm trying to pass a dlarray signal to an stftLayer inside a dlnetwork. The input signal has size:
size(signal) - [1 24 2401]
dims(signal) - 'CBT'
The stfftLayer configuration is:
winLength = 256;
overlap = 128;
fftLength = 256;
stft_net = [sequenceInputLayer(size(signal,1))
stftLayer("Window",winLength,"FFTLength",fftLength,"OverlapLength",overlap,"Name","stft")];
stftNet = dlnetwork(stft_net);
However, when I am trying to use this model, I am receiving the following error:
Error using stftLayer/validateProperties (line 238)
Window length must be greater than one sample.
Error in stftLayer (line 166)
this = validateProperties(this);
^^^^^^^^^^^^^^^^^^^^^^^^
The signal properties are shown below for further clarity
K>> size(signal)
ans =
1 24 2401
K>> dims(signal)
ans =
'CBT'
What could be causing the stftLayer to misinterpret the time dimension and throw the error, even when T is clearly larger than the window length? Any insights would be much appreciated.

 Accepted Answer

The window must be specified as a vector. Did you want a flat window? If so, then,
signal=rand(1,24,2401);
winLength = 256;
overlap = 128;
fftLength = 256;
stft_net = [sequenceInputLayer(height(signal)),
stftLayer("Window",ones(1,winLength),"FFTLength",fftLength,...
"OverlapLength",overlap,"Name","stft")]
stft_net =
2×1 Layer array with layers: 1 '' Sequence Input Sequence input with 1 dimensions 2 'stft' STFT stftLayer

5 Comments

Hi Matt,
Thanks for the answer, however this did not fix the problem. I am still receiving the following error when trying to wrap stft_net in dlnetwork:
>>dlnetwork(stft_net)
Error using dlnetwork/initialize (line 600)
Invalid network.
Error in dlnetwork (line 182)
net = initialize(net, dlX{:});
^^^^^^^^^^^^^^^^^^^^^^^
Caused by:
Layer 'stft': Error using the predict function in layer stftLayer. The function threw an error and could not be executed.
Error using stftLayer/predict (line 183)
Size of input in T dimension must be greater than or equal to window length.
signal=rand(1,24,2401);
winLength = 256;
overlap = 128;
fftLength = 256;
stft_net = [sequenceInputLayer(height(signal)),
stftLayer("Window",ones(1,winLength),"FFTLength",fftLength,...
"OverlapLength",overlap,"Name","stft")]
stft_net =
2×1 Layer array with layers: 1 '' Sequence Input Sequence input with 1 dimensions 2 'stft' STFT stftLayer
stftNet = dlnetwork(stft_net, networkDataLayout(size(signal),'CBT'))
stftNet =
dlnetwork with properties: Layers: [2×1 nnet.cnn.layer.Layer] Connections: [1×2 table] Learnables: [1×3 table] State: [0×3 table] InputNames: {'sequenceinput'} OutputNames: {'stft'} Initialized: 1 View summary with summary.
Or,
signal=rand(1,24,2401);
winLength = 256;
overlap = 128;
fftLength = 256;
stft_net = [sequenceInputLayer(height(signal),MinLength=2041),
stftLayer("Window",ones(1,winLength),"FFTLength",fftLength,...
"OverlapLength",overlap,"Name","stft")]
stft_net =
2×1 Layer array with layers: 1 '' Sequence Input Sequence input with 1 dimensions 2 'stft' STFT stftLayer
stftNet = dlnetwork(stft_net)
stftNet =
dlnetwork with properties: Layers: [2×1 nnet.cnn.layer.Layer] Connections: [1×2 table] Learnables: [1×3 table] State: [0×3 table] InputNames: {'sequenceinput'} OutputNames: {'stft'} Initialized: 1 View summary with summary.
Thanks, Matt! Setting the MinLength resolved the error. Could you explain why that fixed it?
It can't initialize the network unless it has some way of knowing how long your time sequences are.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 30 May 2025

Commented:

on 30 May 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!