2
$\begingroup$

Plots

I want to perform a spectrum analysis of my own voice using the code below.

recObj  = audiorecorder;
recDuration = 10;
disp("Begin speaking.")
recordblocking(recObj,recDuration);
disp("End of recording.")
playerObj = play(recObj);
disp('Properties of playerObj:');
myRecord = get(playerObj)
y = getaudiodata(recObj);
figure(1)
plot(y)


% Spectrum
Fs = 8000;
m = length(y)
n = pow2(nextpow2(m));
y2 = fft(y,n)
f = (0:n-1)*(Fs/n);
mag = 2*abs(y2)/n;

figure(2)
plot(f,mag)
xlabel('Frequency(Hz)')
ylabel('Magnitude')
title('{\bf Audio Spectrum}')
axis([200 max(f) 0 max(mag)])

% Center the periodogram

y0 = fftshift(y2);
f0 = (-n/2:n/2-1)*(8000/n);
power0 = y0.*conj(y0)/n;

figure(3)
plot(f0,power0)
xlabel('Frequency(Hz)')
ylabel('Power')
title('{\bf 0-Centered Periodogram}')



%Plot the magnitude spectrum

figure(4)
mag0 = 2*abs(y2)/n;
plot(f0,mag0)
axis([0 max(f0) 0 max(mag0)])
xlabel('Frequency(Hz)')
ylabel('Magnitude')
title('{\bf Magnitude Spectrum}')

How to interpret these graphs in order to answer these questions:

  1. What (frequency range) has the smallest magnitude? What is the greatest magnitude?
  2. What is the frequency range of your voice at 500 Hz? At 1000 Hz? At 2000 Hz?   

I know how to read plots. But, the symmetrical figures of FFT got me confused.

$\endgroup$
2
  • 1
    $\begingroup$ Did you generate these plots yourself? Do you have the matlab code that did? $\endgroup$
    – Jdip
    Jan 6 at 15:24
  • $\begingroup$ We cannot help you if you don’t provide additional information. We don’t know what the original signals are, how you generate the plots (I’ve been using MATLAB for some time and I can almost guarantee your frequency axes are not correct) and what exactly your question is... $\endgroup$
    – ZaellixA
    Jan 6 at 16:46

1 Answer 1

3
$\begingroup$

Introduction

There is some theory that is missing here. I am not going to go through it ‘cause it is very well established and you can find it in most introductory textbooks on Digital Signal Processing. Just look for the Discrete Fourier Transform (DFT) for more information (or the Discrete Time Fourier Transform - DTFT).

Symmetric spectrum

Omitting the details, the spectrum of a digital real signal is symmetric about the $\frac{f_{s}}{2}$ frequency, which quite often is termed Nyquist frequency, with $f_{s}$ being the sampling rate.

This is the reason in many DFT implementations there is either a function parameter/argument or even a completely different function that returns only the frequency content from $0$ to the Nyquist frequency (it’s like you provide the guarantee that the signal you transform is a real signal).

Effectively, this means you can discard the content of the spectrum after $\frac{f_{s}}{2}$. Please note that it is absolutely necessary to include it in the spectrum if you want to perform the inverse transform (unless a specific implementation is used that generates it internally).

Centred spectrum

Omitting the mathematical formulation here, I’ll state that the spectrum of digital signals is a periodic function with period $f_{s}$. This means that the frequency content from $\frac{f_{s}}{2}$ to $f_{s}$ is identical to that in the range from $-\frac{f_{s}}{2}$ to $0$.

Since the frequency content is identical in the two frequency ranges, you could use the $\left[ -\frac{f_{s}}{2}, 0 \right]$ range instead of $\left[ -\frac{f_{s}}{2}, f_{s} \right]$. This way, you end up with the centre of the spectrum vector being the $0 \, \textrm{Hz}$ frequency.

Remarks

I’ve been using MATLAB for quite some time and I can guarantee that its built-in routines return the spectrum with the negative frequencies after the Nyquist frequency. This means that your frequency axis is incorrect in the Magnitude spectrum, while in the Audio spectrum is correct.

Furthermore, you suggest that the two spectra are the same, but I do see differences between the those in the Audio and Magnitude images, unless of course this is an issue with the image compression that makes them look different. The reason cannot be deduced simply by looking at them so if you don’t provide additional information about the generation of the plots we won’t be able to help.

$\endgroup$
4
  • $\begingroup$ Ah yes good call, I see what you mean in the remarks--- They don't match so if we assume the Periodogram as the PSD is correct, then the plot "Audio Spectrum" is correct, while the plot "Magnitude Spectrum" somehow got mirrored. The OP needs to use fftshift before making that plot! $\endgroup$ Jan 6 at 18:56
  • $\begingroup$ Yes, exactly… the “Magnitude” spectrum has the same axes as the periodogram, but the actual data ($y$ axis) of the “Audio” spectrum, which is not correct. The fftshift is indeed very helpful in making those plots… Thanks for the addition :). $\endgroup$
    – ZaellixA
    Jan 6 at 21:38
  • 1
    $\begingroup$ “the spectrum with the negative frequencies after the Nyquist frequency” Sigh. This is an interpretation. But the DFT is defined for frequencies k=[0, N-1], and that is what MATLAB’s fft returns (and most other FFTs). $\endgroup$ Jan 6 at 23:53
  • $\begingroup$ @CrisLuengo, thanks for the comment. Please feel free to suggest corrections if you see something is not correct. $\endgroup$
    – ZaellixA
    Jan 7 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.