
Computer code is becoming increasingly complex, making it difficult for beginners to understand. Minimal coding offers a possible solution. Here is an example of minimal code for a periodogram.
The periodogram code from the Signal Processing Toolbox in the current version of MATLAB R2026a is 263 lines long, including comment lines. That is a lot compared to a method that actually involves only two steps: (1) calculating the Fast Fourier Transform of the data and (2) calculating the power spectral density from the absolute of the Fourier transform of the data—that’s it. In addition, the next power of two of the data length is required for the FFT. After calculating the spectral density, the frequency axis f and the spectral density axis Pxx are created.
Here is an example of minimal coding—that is, reducing computer code to the absolute minimum. First, we generate a dataset of a composite time series consisting of three sinusoidal oscillations with periods of 50, 15, and 5:
clear, clc, close all Ns = 2.5; Fs = 2; t = 1/Fs:1/Fs:500; t = t'; x = 2*sin(2*pi*t/50) + ... sin(2*pi*t/15) + ... 0.5*sin(2*pi*t/5);
Here are the three steps described above, calculating the FFT of the data series and the power spectral density:
nfft = 2^nextpow2(length(t)); Xxx = fft(x,nfft); Pxx2 = abs(Xxx).^2 /Fs /length(x); Pxx = [Pxx2(1); 2*Pxx2(2:nfft/2)]; f = 0:Fs/(nfft-1):Fs/2; f = f';
Essentially, that’s it, let’s plot the result:
plot(f,Pxx)
xlabel('Frequency')
ylabel('Power')
If you would like to make it look a little nicer, I recommend this slightly more extensive code, which consistently uses graphic attributes according to the name-value scheme:
figure('Position',[100 800 800 500],...
'Color',[1 1 1])
axes('Position',[0.15 0.2 0.7 0.6],...
'FontSize',12), hold on
line(f,Pxx,...
'LineWidth',1,...
'Color',[0.0660 0.4430 0.7450])
xlabel('Frequency')
ylabel('Power Spectral Density')
The code above corresponds to the brief description of periodogram found in textbooks. The much more comprehensive code for MATLAB and Python naturally offers a great many more options but this significantly increases the effort required to learn it.
For over 30 years, I have been striving to keep the code as simple as possible—not only to make it easier for students to understand, but also to facilitate collaborative coding. In my textbooks and my courses, I consistently follow this principle: algorithms are first reduced to a handful of lines of code, as shown above, before a ready-made function—such as PCA in a MATLAB toolbox or a Python package—is used to handle the routine tasks.
References
Trauth, M.H. (2025) MATLAB Recipes for Earth Sciences – Sixth Edition. Springer International Publishing, 567 p, https://doi.org/10.1007/978-3-031-57949-3.
Trauth, M.H. (2024) Python Recipes for Earth Sciences – Second Edition. Springer International Publishing, 491 p., https://doi.org/10.1007/978-3-031-56906-7.
