Home > matlab code, Uncategorized, youtube demo code > plotting the frequency content of a signal

## plotting the frequency content of a signal

A ‘web friendly’ version of this code with figures is available at https://dadorran.wordpress.com/2014/02/20/plotting-frequency-spectrum-using-matlab/

```%% Demonstration of how to plot the Frequency Spectrum of a Signal
%https://www.dropbox.com/s/n4kpd4pp2u3v9nf/tremor_analysis.txt
N = length(signal);
fs = 62.5; % 62.5 samples per second
fnyquist = fs/2; %Nyquist frequency

%% Quick view of double-sided (two-sided) magnitude spectrum
% When roughly interpreting this data half way along x-axis
% corresponds to half the sampling frequency
plot(abs(fft(signal)))
xlabel('Frequency (Bins - almost!)')
ylabel('Magnitude');
title('Double-sided Magnitude spectrum');
axis tight

%% Double-sided magnitude spectrum with frequency axis (in bins)
fax_bins = [0 : N-1]; %N is the number of samples in the signal
plot(fax_bins, abs(fft(signal)))
xlabel('Frequency (Bins)')
ylabel('Magnitude');
title('Double-sided Magnitude spectrum (bins)');
axis tight

%% Single-sided magnitude spectrum with frequency axis in bins
X_mags = abs(fft(signal));
fax_bins = [0 : N-1]; %frequency axis in bins
N_2 = ceil(N/2);
plot(fax_bins(1:N_2), X_mags(1:N_2))
xlabel('Frequency (Bins)')
ylabel('Magnitude');
title('Single-sided Magnitude spectrum (bins)');
axis tight

%% Single-sided magnitude spectrum with frequency axis in Hertz
% Each bin frequency is separated by fs/N Hertz.
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz = bin_vals*fs/N;
N_2 = ceil(N/2);
plot(fax_Hz(1:N_2), X_mags(1:N_2))
xlabel('Frequency (Hz)')
ylabel('Magnitude');
title('Single-sided Magnitude spectrum (Hertz)');
axis tight

%% Single-sided magnitude spectrum with frequency axis normalised
% Normalised to Nyquist frequency.
% Very common to use this method of normalisation in matlab
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_norm = (bin_vals*fs/N)/fnyquist; % same as bin_vals/(N/2)
N_2 = ceil(N/2);
plot(fax_norm(1:N_2), X_mags(1:N_2))
xlabel({'Frequency (Normalised to Nyquist Frequency. ' ...
'1=Nyquist frequency)'})
ylabel('Magnitude');
title('Single-sided Magnitude spectrum (Normalised to Nyquist)');
axis tight

%% Single-sided magnitude spectrum - frequency in rads per sample
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
N_2 = ceil(N/2);
ylabel('Magnitude');
axis tight

%% Double-sided magnitude spectrum showing negative frequencies
% See http://youtu.be/M1bLPZdNCRA for an explanation of negative
% frequencies
X_mags = abs(fftshift(fft(signal)));
bin_vals = [0 : N-1];
N_2 = ceil(N/2);
fax_Hz = (bin_vals-N_2)*fs/N;
plot(fax_Hz, X_mags)
xlabel('Frequency (Hz)')
ylabel('Magnitude');
title('Double-sided Magnitude spectrum (Hertz)');
axis tight

%% Single-sided magnitiude spectrum in decibels and Hertz
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz = bin_vals*fs/N;
N_2 = ceil(N/2);
plot(fax_Hz(1:N_2), 10*log10(X_mags(1:N_2)))
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)');
title('Single-sided Magnitude spectrum (Hertz)');
axis tight

%% Single-sided power spectrum in decibels and Hertz
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz = bin_vals*fs/N;
N_2 = ceil(N/2);
plot(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)))
xlabel('Frequency (Hz)')
ylabel('Power (dB)');
title('Single-sided Power spectrum (Hertz)');
axis tight

%% Single-sided power spectrum in dB and frequency on a log scale
X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz = bin_vals*fs/N;
N_2 = ceil(N/2);
semilogx(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)))
xlabel('Frequency (Hz)')
ylabel('Power (dB)');
title({'Single-sided Power spectrum' ...
' (Frequency in shown on a log scale)'});
axis tight

```
1. May 14, 2015 at 6:27 am

dear David,

Firstly thanks for the explanation.

I have few doubts regarding the Y-AXIS units?

I am using Oceanographic data. I have 122 days temperature data, every day one sample, my sampling frequency is fs=1;

My temperature ranges from -3 to 3 degree centigrade.

After plotting spectrum using your suggestions , I got 25 units on Y-AXIS Limits.
Now what are the units for Y-Axis??

• May 14, 2015 at 12:48 pm

All signal can be decomposed into a set of sinusoids – see https://www.youtube.com/watch?v=YS6jaqeXVok (or more correctly complex exponentials) . The y-axis gives you indication of the amplitude of the sinusoids (divide by N/2 to get the exact ampltitude) present in a signal – see https://www.youtube.com/watch?v=dM1y6ZfQkDU.

To get the units you need to understand what the amplitude of the data you have corresponds to. For example, if you had a sequence of data in which a value of 1 corresponded to a voltage measurement of 1 volts then the units of the y-axis (if you divided divided by N/2) would be volts. So say you got two ‘spikes’ in your magnitude spectrum of amplitude 0.3 and 0.6 (after dividing by N/2) at 100 and 200 Hertz you would be able to say that that signal comprised of two sinusoidal signals of amplitude 0.3 and 0.6 volts;

2. May 21, 2015 at 5:10 am

David,

I still did not understand.

As we know that water temp (WT) – Air temp (AT) ranges from -2 to 4 degrees (in tropical regions).
Suppose WT are hotter than AT in tropics (most of the time).

when I plot Spectrum for both parameters 1. WT , 2. WT-AT on the same plot. The magnitude for WT-AT is higher than WT magnitude.

Please suggest me where am I going wrong?

N = length(wt2);
fs = 1; %number of samples per day
fnyquist = fs/2; %Nyquist frequency
figure;
T = 1./fax_norm;
N_2 = ceil(N/2);
X_mags_td08 = abs(fft(wt2-at2));

plot(T(1:N_2), X_mags_wt2(1:N_2),’linewidth’,2,’color’,’b’),hold on;
plot(T(1:N_2), X_mags_td08(1:N_2),’linewidth’,2,’color’,’r’);

The code I followed is same everything.
I am posting data also please have look at it.

time Air temperature water temperature Air Sea Temperature difference
1-Jun-11 28.81 29.72 0.91
2-Jun-11 29.73 29.87 0.14
3-Jun-11 28.99 30.11 1.12
4-Jun-11 30.20 30.23 0.03
5-Jun-11 30.30 30.52 0.22
6-Jun-11 28.60 30.49 1.90
7-Jun-11 28.85 30.25 1.40
8-Jun-11 28.35 29.96 1.61
9-Jun-11 29.02 29.79 0.77
10-Jun-11 29.33 29.51 0.19
11-Jun-11 28.97 29.75 0.77
12-Jun-11 28.08 29.65 1.57
13-Jun-11 26.99 29.25 2.26
14-Jun-11 27.71 29.33 1.63
15-Jun-11 28.38 29.18 0.79
16-Jun-11 28.66 28.92 0.26
17-Jun-11 29.63 29.12 -0.51
18-Jun-11 29.64 29.23 -0.41
19-Jun-11 29.63 29.43 -0.20
20-Jun-11 29.55 29.41 -0.15
21-Jun-11 29.35 29.31 -0.04
22-Jun-11 29.18 29.26 0.09
23-Jun-11 29.37 29.34 -0.02
24-Jun-11 29.38 29.33 -0.04
25-Jun-11 27.86 29.27 1.40
26-Jun-11 29.01 29.15 0.14
27-Jun-11 29.21 29.15 -0.06
28-Jun-11 29.26 29.18 -0.07
29-Jun-11 29.38 29.29 -0.09
30-Jun-11 29.33 29.28 -0.04
1-Jul-11 29.45 29.38 -0.06
2-Jul-11 28.47 29.41 0.94
3-Jul-11 27.76 29.34 1.58
4-Jul-11 27.88 29.24 1.36
5-Jul-11 27.89 29.25 1.36
6-Jul-11 28.06 29.17 1.11
7-Jul-11 28.80 29.21 0.41
8-Jul-11 28.95 29.26 0.31
9-Jul-11 29.26 29.27 0.02
10-Jul-11 29.05 29.29 0.24
11-Jul-11 27.80 29.23 1.43
12-Jul-11 28.64 29.30 0.66
13-Jul-11 29.01 29.43 0.41
14-Jul-11 28.93 29.41 0.48
15-Jul-11 28.77 29.31 0.54
16-Jul-11 28.88 29.30 0.42
17-Jul-11 28.55 29.30 0.75
18-Jul-11 28.71 29.23 0.51
19-Jul-11 29.13 29.06 -0.07
20-Jul-11 29.19 28.92 -0.28
21-Jul-11 29.05 28.89 -0.16
22-Jul-11 29.03 28.94 -0.10
23-Jul-11 28.94 29.02 0.07
24-Jul-11 27.72 29.05 1.33
25-Jul-11 28.62 29.13 0.50
26-Jul-11 28.76 29.28 0.52
27-Jul-11 28.82 29.42 0.59
28-Jul-11 28.45 29.63 1.17
29-Jul-11 28.57 29.61 1.04
30-Jul-11 29.15 29.63 0.48
31-Jul-11 29.58 29.72 0.14
1-Aug-11 29.68 29.90 0.22
2-Aug-11 28.53 30.06 1.53
3-Aug-11 28.46 29.81 1.35
4-Aug-11 29.12 29.39 0.27
5-Aug-11 29.14 29.19 0.04
6-Aug-11 29.16 29.14 -0.02
7-Aug-11 29.32 29.07 -0.25
8-Aug-11 29.32 29.07 -0.25
9-Aug-11 29.26 29.05 -0.21
10-Aug-11 29.23 29.06 -0.17
11-Aug-11 26.53 28.92 2.39
12-Aug-11 27.08 28.61 1.53
13-Aug-11 28.80 28.66 -0.14
14-Aug-11 28.89 28.79 -0.10
15-Aug-11 29.04 28.86 -0.18
16-Aug-11 28.38 28.94 0.56
17-Aug-11 28.11 29.03 0.93
18-Aug-11 27.43 28.95 1.53
19-Aug-11 27.71 28.85 1.13
20-Aug-11 27.72 28.94 1.22
21-Aug-11 27.35 28.85 1.50
22-Aug-11 26.93 28.65 1.72
23-Aug-11 27.81 28.62 0.81
24-Aug-11 27.17 28.58 1.41
25-Aug-11 27.38 28.51 1.14
26-Aug-11 27.99 28.61 0.62
27-Aug-11 28.49 28.67 0.17
28-Aug-11 27.79 28.77 0.98
29-Aug-11 28.11 28.97 0.86
30-Aug-11 28.17 29.03 0.86
31-Aug-11 27.58 28.99 1.42
1-Sep-11 28.56 28.89 0.33
2-Sep-11 28.27 28.88 0.61
3-Sep-11 27.13 28.84 1.71
4-Sep-11 27.75 28.78 1.03
5-Sep-11 28.23 28.76 0.52
6-Sep-11 27.77 28.67 0.90
7-Sep-11 28.88 28.48 -0.40
8-Sep-11 28.73 28.66 -0.07
9-Sep-11 28.84 28.73 -0.12
10-Sep-11 28.68 28.64 -0.04
11-Sep-11 28.34 28.70 0.36
12-Sep-11 28.49 28.83 0.34
13-Sep-11 27.31 28.94 1.63
14-Sep-11 28.23 28.81 0.59
15-Sep-11 28.76 28.88 0.11
16-Sep-11 28.91 28.88 -0.03
17-Sep-11 29.02 28.94 -0.09
18-Sep-11 29.14 29.02 -0.12
19-Sep-11 27.95 29.04 1.08
20-Sep-11 26.99 28.73 1.75
21-Sep-11 27.46 28.48 1.02
22-Sep-11 28.42 28.39 -0.03
23-Sep-11 28.71 28.48 -0.23
24-Sep-11 28.86 28.56 -0.30
25-Sep-11 28.88 28.84 -0.04
26-Sep-11 28.90 28.97 0.07
27-Sep-11 28.95 29.08 0.13
28-Sep-11 28.98 29.19 0.21
29-Sep-11 29.22 29.40 0.19
30-Sep-11 29.38 29.44 0.06