Changing to DB in frequency spectrum from power

25 次查看(过去 30 天)
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 written some code for it and shifted it to 0 to both have a positive and negative axis, the problem becomes when I try to convert it to DB such as the question asks, when I do this I really get an ugly graph but I can see that a frequency of 6000 was added to the sample.
The code is shown under here
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
n=length(nokia)
f=(0:n-1)*(fs/n)
power=((abs(fft_nokia).^2/n))
db=mag2db(f)
plot(f,power)
xticks([0 5000 10000 15000 20000 25000 30000 35000 40000 45000])
xticklabels({0 5000 10000 15000 20000 25000 30000 35000 40000 45000})
xlabel('Frequency Hz')
ylabel('Power')
y0 = fftshift(fft_nokia); % shift y values
f0 = (-n/2:n/2-1)*(fs/n); % 0-centered frequency range
power0 = ((abs(y0).^2)/n);
% 0-centered power
plot(f0,power0)
xlabel('Frequency')
ylabel('Power')
xlim([-6500 6500])
ylim([0 160])
The problem with this is that when I try to convert the power to db or do it I get like an inverted V whilst I expected something much cleaner since my original power signal looks like this: ( See picture attached) Whilst with the DB I get nothing for when I implement it in y0 or f0.
My question to you use, how can I change my code to properly display the Decibel of the signal.
Thank you for reading and hopefully responding

回答(1 个)

Mohamad
Mohamad 2020-11-22
db=10*log10((fft_nokia.*conj(fft_nokia))/n);

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by