How to create a spectrum of an audiofile in Matlab 2020B

41 views (last 30 days)
Well as a homework assignment we have been given the following question:
Plot the spectrum of the f file, by taking the fft. Apply the function ’fftshift’ to the spectrum, to make sure that k = 0 is in the center of the plot. In pseudo-code:
X[k] = fftshift(fft(x[n]))
For simplicity, put the frequency ν [Hz] on the horizontal axis instead of k, and make sure it has the correct values. On the vertical axis, plot the magnitude of the spectrum in decibel (10log10(|X[k]|)). Label the axis properly. Can you identify what the frequency of the noise is that we added?
Well, I have tried to write some code for the spectrum to start off and plotting the normal plot. The problem with the current plot is that when I plot it, there is a big signal at a negative hertz which is impossible.
clc; clear all; close all;
%%Reading & plotting nokiatune.wav file
nokia = audioread('nokia_tune_noise.wav');
figure(1)
plot(nokia);
xlabel('samples','fontsize',12,'fontweight','b');
ylabel('Amplitude','fontsize',12,'fontweight','b');
title('Audiofile we should use','fontsize',14,'fontweight','b');
%fouriertransform of nokia
fft_nokia=(fft(nokia))
fs=44100 % already given in Hz
f_nokia = fs / 2 * linspace(-1,1,fs)
%%Plotting frequency spectrum
% Normal sound
figure(3)
plot(f_nokia, abs(fft_nokia([1+numel(fft_nokia)-numel(f_nokia):end])));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Normal Signal','fontsize',14,'fontweight','b');
My question to you is, what should I change so that it becomes positive and correct. FS=44100
Thanks already for the support!

Answers (1)

Star Strider
Star Strider on 22 Nov 2020
The problem with the current plot is that when I plot it, there is a big signal at a negative hertz which is impossible.
Not in a two-sided Fourier transform. The frequencies of the Fourier transform are defined as going from -Inf to +Inf, with the spectrum values on the negative frequencies being the complex-conjugate of the spectrum values on the positive frequencies. (Taking the absolute value produces a mirror-image spectrum on both sides with respect to the centre frequency.) While only positive frequencies are routinely displayed, the negative frequencies always exist.
The Laplace transform, in contrast, is defined only for positive frequencies in complex s-space.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!