Changing to DB in frequency spectrum from power

22 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Mohamad
Mohamad il 22 Nov 2020
db=10*log10((fft_nokia.*conj(fft_nokia))/n);
  1 Commento
Tim van Schaik
Tim van Schaik il 22 Nov 2020
Weird, when I try this I simply get the same value and an empty value for the y0

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by