can't get the correct plot of fft - matlab

I have a problem with the plot of the spectrum, I have an audio file on which I run the fft function and try to plot its spectrum :
fileName = 'stuff.wav' ;
[y, Fs] = audioread(fileName);
Y = fft(y(:,1));
Y = Y / length(y);
plot(fftshift(abs(Y));
and here is what I get :
the magnitude is correct but the frequencies aren't.
so I've wrote a small script to test this :
fs =8000;
t = 0:1/fs:10;
x = 3*sin(2*pi*5*t);
U = abs(fft(x));
stem(t,fftshift(U));
axis([-20 20 -5 5]);
and the result is :
why the peak is in the right place meaning 5 but I was excpecting a second one in -5 negative frequencieswhy I can see it and how can I scale correctly the X axis ?
thanks for your help !

You are ploting the FFT result (U) versus time, which doesn't make much sense. First generate the frequency axis values, and then use that when plotting U:
%// Same as in your code:
fs = 8000;
t = 0:1/fs:10;
x = 3*sin(2*pi*5*t);
U = abs(fft(x));
%// Do the following changes to your code:
f = -fs/2:fs/(numel(t)-1):fs/2; %// frequency axis
stem(f, fftshift(U)); %// plot U versus frequency
axis([-10 10 0 15e4]); %// zoom in to see -5 Hz and 5 Hz values

Related

How to calculate modulation transfer function of a gaussian curve in MATLAB?

I am trying to find modulation transfer function of a gaussian fitted curve by using MATLAB.
The gaussian curve is as below:
x- axis is distance(form -15mm to 15mm) and y axis is count(Magnitude).
I used the following code to find find fourier transfrom of gaussian curve
FFT_y = fft(y); %take fourier transform
FF_mag = abs(FFT_y )/(length(FFT_y )); %find magnitude
FF_mag = (FF_mag-min(FF_mag))./(max(FF_mag)-min(FF_mag)); %normalize magnitude
I cropped FF_mag by using the following codes
FF_mag_nw = FF_mag(1:(length(FF_y)/32));
plot(FF_mag_nw);
I used 32 in above code to get main portion of graph and I got MTF plot as below:
I am confused about X-axis. What will be the range of X-axis in lines per mm?
Can anyone help me give an idea to calculate X-axis of MTF plot?
Thanks in Advance!
Manu
The matlab manual is very specific about the calculation. For example, check this out
dx = 0.1
x = -15:dx:15;
Fs = 1/dx; % Sampling frequency
L = length(x); % Signal length
sigma = 5;
amp = 437500;
y = amp/sqrt(2*pi*sigma^2) * exp(-x^2/(2*sigma^2));
n = 2^nextpow2(L);
Y = fft(y,n);
f = Fs*(0:(n/2))/n;
P = abs(Y/n);
plot(f,P(1:n/2+1))
title('Gaussian Pulse in Frequency Domain')
xlabel('Frequency f [in units of 1/dx]')
ylabel('|P(f)|')
How many points did you use for the real space gaussian. If this number is N, to transform the fft x axis in (1/mm) you should divide by N.

Correct frequency axis using FFT

How can I get the correct frequency vector to plot using the FFT of MATLAB?
My problem:
N = 64;
n = 0:N-1;
phi1 = 2*(rand-0.5)*pi;
omega1 = pi/6;
phi2 = 2*(rand-0.5)*pi;
omega2 = 5*pi/6;
w = randn(1,N); % noise
x = 2*exp(1i*(n*omega1+phi1))+4*sin(n*omega2+phi2);
h = rectwin(N).';
x = x.*h;
X = abs(fft(x));
Normally I'd do this :
f = f = Fs/Nsamples*(0:Nsamples/2-1); % Prepare freq data for plot
The problem is this time I do not have a Fs (sample frequency).
How can I do it correctly in this case?
If you don't have a Fs, simply set it to 1 (as in one sample per sample). This is the typical solution I've always used and seen everybody else use. Your frequencies will run from 0 to 1 (or -0.5 to 0.5), without units. This will be recognized by everyone as meaning "periods per sample".
Edit
From your comment I conclude that you are interested in radial frequencies. In that case you want to set your plot x-axis to
omega = 2*pi*f;

Matlab Recreating freqz, normalizing x axis and getting half of plot

I have a function which is basically recreating the freqz command in matlab. I have figured out how to plot the entire transform of my frequency response, but I only need half of it, and I need to normalize it from pi to 1 (where 0:pi represents my x axis, and I want that to go to 0:1). The code is here:
function freqrespplot(b, a)
hb = fft(b,512);
ha = fft(a,512);
magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);
x = linspace(0,2*pi, 512);
subplot(2,1,1);
plot(x,magh,'g');
subplot(2,1,2);
plot(x,angh,'r');
end
In the example of b = [1 2 2], a = [0 1 .8], plots like the following:
freqrespplot([1 2 2], [0 1 .8]);
Magnitude
and my corresponding phase plot is
I want the x-axis (omega) to go from 0 to 1 in both cases, and correspond to 0 to pi by taking only half of the graph, like this if possible:
You only need some small changes, marked with comments in the code below:
function freqrespplot(b, a)
hb = fft(b,512);
ha = fft(a,512);
magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);
x = linspace(0,2, 513); %// 2, not 2*pi. And 513. Last value will be discarded
x = x(1:end-1); %// discard last value to avoid having 0 and 2*pi (they are the same)
subplot(2,1,1);
plot(x(1:end/2),magh(1:end/2),'g'); %// plot only half
subplot(2,1,2);
plot(x(1:end/2),angh(1:end/2),'r'); %// plot only half
end
With your example b and a this produces
You may want to include one additional sample to reach the right edge of the graph. I comment only differences with the above code:
function freqrespplot(b, a)
hb = fft(b,512);
ha = fft(a,512);
magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);
x = linspace(0,2, 513);
x = x(1:end-1);
subplot(2,1,1);
plot(x(1:end/2+1),magh(1:end/2+1),'g'); %// plot only lower half plus one value
subplot(2,1,2);
plot(x(1:end/2+1),angh(1:end/2+1),'r'); %// plot only lower half plus one value
end
Compare the resulting graph (rightmost part):

Low pass filter implementation Correct or wrong?

I've been working on 2 sensors signals measuring vibrations of a rotating shaft. Since there is residual noise in the signal. I tried to filter it by detrending, zero padding and applying low pass filter. Below I'm attaching the graphs of the signal before and after filtering. There is a huge variation in the signal after filtering that makes me think if I'm really doing it in the right way.
My Matlab code is
X = xlsread(filename,'F:F');
Y = xlsread(filename,'G:G');
%Calculate frequency axis
fs = 1e6 ; % Sampling frequency (Hz)
NFFT = 2^nextpow2(length(X)); % Zero padding to nearest N power 2
df = fs/NFFT;
dt = 1/df;
%Frequency Axis defintion
f = (-(fs-df)/2:df:(fs-df)/2)';
X(2^ceil(log2(length(X))))=0;
Y(2^ceil(log2(length(Y))))=0;
%calculate time axis
T = (dt:dt:(length(X)*dt))';
subplot(2,2,1)
plot(T,X);
xlabel('Time(s)')
ylabel('X amplitude')
title('X signal before filtering')
subplot(2,2,2)
plot(T,Y);
xlabel('Time(s)')
ylabel('Y amplitude')
title('Y signal before filtering')
X = detrend(X,0); % Removing DC Offset
Y = detrend(Y,0); % Removing DC Offset
% Filter parameters:
M = length(X); % signal length
L = M; % filter length
fc = 2*(38000/60); % cutoff frequency
% Design the filter using the window method:
hsupp = (-(L-1)/2:(L-1)/2);
hideal = (2*fc/fs)*sinc(2*fc*hsupp/fs);
h = hamming(L)' .* hideal; % h is our filter
% Zero pad the signal and impulse response:
X(2^ceil(log2(M)))=0;
xzp = X;
hzp = [ h zeros(1,NFFT-L) ];
% Transform the signal and the filter:
X = fft(xzp);
H = fft(hzp)';
X = X .* H;
X = ifft(X);
relrmserrX = norm(imag(X))/norm(X); % checked... this for zero
X = real(X)';
% Zero pad the signal and impulse response:
Y(2^ceil(log2(M)))=0;
xzp = Y;
hzp = [ h zeros(1,NFFT-L) ];
% Transform the signal and the filter:
Y = fft(xzp);
H = fft(hzp)';
Y = Y .* H;
Y = ifft(Y);
relrmserrY = norm(imag(Y))/norm(Y); % check... should be zero
Y = real(Y)';
I plotted the after filtering and as you can see in the picture there is a clear deviation. I want to only filter noise but the signal seem to loose other components and I'm little confused if thats the right way of doing filtering.
Any suggestion, hints or ideas would be helpful.
In the end I want to plot X vs Y to give orbits of the shaft vibration. Please also find below another picture of the unfiltered and filtered orbit. As you can see in the picture there is also change in the orbits from the original one (left image with lot of noise).
P.S.: I don't have DSP tool box
There is no problem with your FFT and IFFT.
You can test your code with a simple sine wave, with a very low frequency. The final output should be (almost) the same sine wave, since you are low-pass filtering it.
You've defined X (and Y) from 0 to some value. However, you've defined H from - (L-1)/2 to some positive value. Mathematically, this is fine, but you are simply taking an fft of H. Matlab thinks this has the same time-scale as X, when you multiply the ffts together!
So, in reality, you've taken the fft of XHfft(delta(t-d)), where d is the resulting time-shift. You can either undo this time-shift in the frequency domain, or the time-domain.

FFT in MATLAB: wrong 0Hz frequency

I want to use fft in MATLAB to analize some exprimental data saved as an excell file.
my code:
A=xlsread('Book.xls'); G=A'; x=G(2, : );
N=length(x);
F=[-N/2:N/2-1]/N;
X = abs(fft(x-mean(x),N))
X = fftshift(X);
plot(F,X)
But it plots a graph with a large 0Hz wrong component, my true frequency is about 395Hz and it is not shown in the plotted graph.
Please tell me what is wrong.
Any help would be appreciated.
Assume we read the signal from file:
G = xlsread('Book.xls');
t = G(:,1);
x = G(:,2);
N = length(x);
First we estimate the sampling frequency from the time axis, and build the frequency vector:
Fs = 1 ./ abs( t(2)-t(1) );
F = (-N/2:N/2-1)*Fs/N;
then compute the FFT and plot:
X = abs( fft(x-mean(x),N) );
X = fftshift(X);
stem(F,X)
finally find the peak and the corresponding frequency:
>> [~,ind] = max(X);
>> F(ind)
ans =
-400
you might want to zoom-in near the origin to see things more clearly:
xlim([-1000 1000])