Octave/MATLAB: Index out of bounds error when plotting FFT amplitude of a wavelet
00:40 25 May 2026

I am writing an Octave script to visualize the frequency spectrum of a Ricker wavelet. My global time array has nt = 512 samples, but my wavelet is defined with a shorter length of nw = 101 samples. When I attempt to plot the frequency spectrum up to the Nyquist frequency (nt/2), the execution halts with an index out of bounds error.

Here is the minimal reproducible code that triggers the error:

% Global parameters

dt = 0.002;

nt = 512;

f0 = 30;

% Wavelet generation (101 samples)

nw = 101;

t_w = (-(nw-1)/2 : (nw-1)/2) * dt;

tau = pi * f0 * t_w;

w = (1 - 2*tau.^2) .* exp(-tau.^2); % Length is 101

% Frequency spectrum calculation

W_amp = abs(fft(w));

freq = (0:nt-1) / (nt * dt);

% The error occurs on the following line:

plot(freq(1:nt/2), W_amp(1:nt/2)/max(W_amp));

matlab signals processing fft octave