Guide Pwelch Usage - UQ-Communication-Systems/public GitHub Wiki
This guide shows how to plot the spectrum of a signal using a welch estimator. It assumes the signal already exists, and it could be simulated or loaded from a file (see Guide-Reading-Data).
If the signal is a complex signal, then you should use a two sided spectrum. If the signal is real, then you should use a one sided spectrum.
Matlab/Octave
Two sided spectrum
Normally used for complex signals (for real signals positive and negative side are the same (symmetric)).
Use the pwelch function as follows (This allows you to customise your spectrum plots, and also works in older versions of MATLAB)
fs = 2.048e6 % the sample rate
[fxx, f]=pwelch(z, [],[],[], fs);
plot(f-(mean(f)), fftshift(20*log10(fxx)))
In later versions, you can use just the pwelch function with specific arguments to get a similar spectrum.
pwelch(z, [], [], [], fs, 'shift', 'db'); % Octave pwelch with centred spectrum
pwelch(z, [], [], [], fs, 'centered', 'psd'); % MATLAB 2013+ pwelch with centred spectrum
One sided spectrum
If you are using a real signal (baseband), then you should use
[fxx, f]=pwelch(z, [],[],[], FS);
plot(f, 20*log10(fxx)) % note: assuming z is a 'voltage' signal, therefore to get power need to use 20
Python
Two Sided Spectrum
If you are using Python, the same function is shown below:
from scipy import signal
fs = 2.048e6
f, Pxx = signal.welch(z, fs, nperseg=1024, return_onesided=False)
plt.plot(np.fft.fftshift(f), np.fft.fftshift(20*np.log10(Pxx)))
Using fftshift will make sure that you don't get a line across your graph.
One Sided Spectrum
If the signal is real, use a one sided spectrum
fs = 2.048e6
f, Pxx = signal.welch(z, fs, nperseg=1024)
plt.plot(f, 20*np.log10(Pxx))