I have this plot:
[x,fs]=audioread('audio.ogg');
x=x(1:round(end/10));
n=0:length(x)-1;
s=cos(2*pi*1.25*n).';
x1=x+0.01*s;
y=abs(fft(x1));
plot(y)
And I want to express the time axis in kHz. How can I implement that on Matlab?
Thank you in advance!

 Réponse acceptée

Daniel M
Daniel M le 23 Oct 2019
y is not a time series. Hz are not a unit of time. You cannot express something in the frequency domain as a function of time, because it is not a function of time. (The only thing that might make sense is to plot the magnitude of the fourier transform as a function of the period, which is the inverse of the frequency. But this is trivial, and is still not a time-domain representation of your signal).
Perhaps you mean to plot x in the time domain.
[x,fs]=audioread('audio.ogg');
x=x(1:round(end/10));
t = 0:1/fs:(length(x)-1)/fs; % create a time vector
figure
plot(t,x)
xlabel('Time (s)')
Otherwise, I suggest you read the documentation and examples on fft.

3 commentaires

Daniel M
Daniel M le 23 Oct 2019
I just interpreted your question another way, and realize you might want to plot y against a vector of frequencies. This is a mix-up of terminology. The independent variable (typically the x-axis) is not called "the time axis". If this is what you want, then I still recommend looking at the documentation for fft as the first example shows you exactly how to create a vector of frequencies.
I get it!!
So for solve my problem, what I have done is this:
[x,fs]=audioread('audio.ogg');
x=x(1:round(end/10));
n=0:length(x)-1;
s=cos(2*pi*1.25*n).';
x1=x+0.01*s;
y=abs(fft(x1));
f=fs*(0:length(y)-1)/length(y);
plot(f,y)
I converted the horizontal axis into an axis that goes from 0 to fs (almost fs).
Thank you very much!!
Daniel M
Daniel M le 24 Oct 2019
Modifié(e) : Daniel M le 24 Oct 2019
It would appear that the frequencies would go from zero up to the sampling frequency, but it does not. fft actually returns the frequencies in the following order: zero, positive frequencies up to the Nyquist frequency (half the sampling frequency), followed by the negative frequencies (ascending). You can see this if you look at the frequency spectrum of a sine wave (with a DC offset), and compare it to the same signal with an fftshift.
So for your case, the following code works. Note I'm only taking the positive side of the frequency spectrum, because that is what is physically interpretable.
clear; clc; close all;
load handel
% x = y(1:round(end/10));
x = y;
n = 0:length(x)-1;
t = 0:1/Fs:(length(x)-1)/Fs;
s = cos(2*pi*1.25*n).';
s = cos(2*pi*1.25*t).';
x1 = detrend(x+0.01*s);
figure
plot(x1)
xlabel('Time [s]')
ylabel('Magnitude')
X = abs(fft(x1));
L = length(X);
% just take positives frequencies
f = Fs*(0:floor(L/2))/L;
X2 = X(1:floor(L/2)+1);
figure
plot(f,X2);
xlabel('Frequency [Hz]')
ylabel('Magnitude')
title('Single sided amplitude spectrum')

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Signal Processing Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by