how can i fourier transform with negative frequency uncut in matlab? - matlab

clear all;
Fs=100; %sampling rate
t=-10:(1/Fs):10;
for i=1:20*Fs+1
if -10<t(i) && t(i)<-9
x(i)= -1;
elseif -9<t(i) && t(i)<-8
x(i)=1;
elseif i>2*Fs+1
x(i)=x(i-(2*Fs));
shown above is the code for rectangular function with its domain [-10 10] and period of 2
and here is my positiveFFT function
function [X,freq]=positiveFFT(x,Fs)
N=length(x); %get the number of points
k=0:N-1; %create a vector from 0 to N-1
T=N/Fs; %get the frequency interval
freq=k/T; %create the frequency range
X=fft(x)/N*2; % normalize the data
%only want the first half of the FFT, since it is redundant
cutOff = ceil(N/2);
%take only the first half of the spectrum
X = X(1:cutOff);
freq = freq(1:cutOff);`
by doing this, i made a magnitude spectrum of this rectangular wave, but only when the frequency is positive.
How can i plot a whole magnitude spectrum that includes negative frequencies??

If I understand you correctly, you want to obtain a vector of frequencies form -Fs/2 to Fs/2 and the corresponding magnitude of the spectrum. If so, you should use fftshift, for example:
N=length(x); %get the number of points
k=0:N-1; %create a vector from 0 to N-1
T=N/Fs; %get the frequency interval
freq=fftshift(k/T); %create the frequency range
X=fftshift(fft(x))/N*2; % normalize the data

Related

FFT, but with a linear wavelength scale

When the FFT is performed on a function of time u in Matlab, a complex spectrum uf is returned. To plot the spectral amplitude abs(uf) against its frequency content, a frequency grid can be made to accommodate uf. I can associate a wavelength grid with the frequency grid, and plot uf against that also. The spacing between each element in the frequency array is constant, but since wavelength ~ 1/frequency, the spacing between each point in the wavelength array varies over the array index. I am curious if there is a way to take the FFT of a function of time to yield a spectrum in wavelength that has constant spacing. Here is my code in Matlab:
clc;
close all;
clear all;
lam = 800e-9; % Wavelength (m)
c = 3e8; % Light speed (m/s)
nt = 8192; % Temporal grid resolution
T = 400*1e-15; % Temporal grid size (s)
dt = T/nt; % Temporal pixel spacing
df = 1/(nt*dt); % Frequency pixel spacing
ff = [(0:nt/2-1) (-nt/2:-1)]*df; % Frequency grid
ff = fftshift(ff);
wav = c./ff; % Wavelength array (spacing is not constant between each element)
for k = 1:nt
tt(k) = (-nt/2+k-1)*dt; % Time array
u(k) = cos(2*pi*c/lam*tt(k)); % Function of time
end
%Now I can take FFT:
uf = fftshift(fft(u)); % The spectrum of my function. The FFT has yielded a spectrum associated with a frequency array of linearly spaced elements (ff).
Both plots of spectral amplitude vs. wavelength and vs. frequency yield good results.
figure(1)
plot(ff,abs(uf))
title('Spectral amplitude vs frequency')
xlabel('Frequency (Hz)')
ylabel('Spectral amplitude')
figure(2)
plot(wav,abs(uf))
title('Spectral amplitude vs wavelength')
xlabel('Wavelength (m)')
ylabel('Spectral amplitude');
But my wavelength array does not have constant spacing:
figure(3)
plot(ff)
title('Frequency array')
ylabel('Frequency (Hz)')
xlabel('Index')
figure(4)
plot(wav)
xlim([(nt/2 +1) (nt/2 + 100)])
title('Wavelength array')
ylabel('Wavelength (m)')
xlabel('Index')
You should make a linearly spaced wavelength array and interpolate your data to find the linearly spaced y values.

Do I need to call fftshift before calling fft or ifft?

In the book "Computational Fourier Optics, A Matlab Tutorial" by David Voelz, it is written that a call to fftshift is needed before a call to fft or ifft, but in the MATLAB documentation of fftshift it's only written that this command
rearranges the outputs of fft, fft2, and fftn by moving the zero-frequency component to the center of the array.
There is no mention in documentation that this command should be called before the call to fft, and I saw some examples that call fft without a call to fftshift beforehand.
My question is: Whether or not fftshift needed to be called before a call to fft or ifft?
If fftshift doesn't need to be called before a call to fft, and only after a call to fft, then when should we use (if at all) the command ifftshift with relation to a calculation of the fft of a data set?
The matlab fft only computes half of the frequency spectrum (positive frequencies and the zero frequency if your number of samples is odd) in order to save computation time.
Then, the second half of the frequency spectrum (which is the complex conjugate of the first half) is just added at the end of this vector.
So what you get after a fft is the following vector:
0 1 2 3 ... Fs/2 -Fs/2 ... -3 -2 -1
<----------------> <------------------>
positive freq negative freq
where Fs is the frequency sample.
Now, what fftshift does is just shifting the negative frequency bins (2nd part of the frequency spectrum) at the beginning of your vector so that you can display a nice frequency spectrum starting at -Fs/2 and ending at +Fs/2.
The shifted vector becomes:
-Fs/2 ... -3 -2 -1 0 1 2 3 ... Fs/2
<------------------> <---------------->
negative freq positive freq
So, to answer your question, no you don't need to use fftshift after or before a call to fft or ifft. But if you used a fftshift on your vector, you should undo it by applying an ifftshift or fftshift. (I think both calls are equivalent.)
If you read onwards in the documentation on fftshift:
"It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum."
The shift is not necessary to perform the fft, but it is handy to visualise the Fourier transform. Whether to use fftshift or not is thus dependent upon whether you want to visualise your transform or not.
Note that ifftshift is different than fftshift because it shifts the negative back to the positive. Assume a simple 3 bin unit impulse in frequency domain before fftshift
[0, exp(-jw1), exp(-(jw1-pi)=exp(-jw1+pi)];
fftshift gives
[exp(-jw1+pi)], 0, exp(-jw1)];
you can see the discontinuity in phase. If you perform ifftshift, negative freq is shifted back to positive:
[0, exp(-jw1), exp(-jw1+pi)];
whereas, fftshift again gives:
[exp(-jw1), exp(-jw1+pi), 0];
one can see the phase monotonicity is not the same, (aka, fftshift-ifftshift case gives [decrease, increase] and fftshift-fftshift case gives [increase, decrease] in phase).
Given the title of the book you are studying, I assume you are working with images. Then the proper way is to call both fftshift and ifftshift before and after you call [i]fft.
Your code should look like
spectrum = fftshift(fft2(ifftshift(myimage))
It is quite the same when applying the inverse Fourier transform
myimage = fftshift(ifft2(ifftshift(spectrum))
The simple answer is call to fftshift not needed before a call to fft. fftshift does not influence the calculation of Fast fourier transform. fftshift rearranges values inside a matrix. For eg:
cameraman = imread('cameraman.tif');
fftshifted_cameraman = fftshift(cameraman);
subplot(1,2,1); imshow(cameraman); title('Cameraman');
subplot(1,2,2); imshow(fftshifted_cameraman); title('FFTShifted Cameraman');
It depends on what you are going to do with the transformed data. If you don't perform an fftshift before transforming, the fft result will have every other value multiplied by -1. This doesn't matter if you plan to view the magnitude or magnitude squared of the result. However, if you plan to compare adjacent spectral values and phase is important, you'll need to apply fftshift before transforming to avoid the alternating sign.
Here is a MATLAB script I use to test basic sound analysis functions of MATLAB including fftshift() in displaying the output of fft().
if ~exist('inputFile', 'var')
inputFile = 'vibe.wav';
end
[inputBuffer, Fs] = audioread(inputFile);
fileSize = length(inputBuffer);
numSamples = 2.^(ceil(log2(fileSize))); % round up to nearest power of 2
x = zeros(numSamples, 1); % zero pad if necessary
x(1:fileSize) = inputBuffer(:,1); % if multi-channel, use left channel only
clear inputBuffer; % free this memory
clear fileSize;
t = linspace(0, (numSamples-1)/Fs, numSamples)';
f = linspace(-Fs/2, Fs/2 - Fs/numSamples, numSamples)';
X = fft(x);
plot(t, x);
xlabel('time (seconds)');
ylabel('amplitude');
title(['time-domain plot of ' inputFile]);
sound(x, Fs); % play the sound
pause;
% display both positive and negative frequency spectrum
plot(f, real(fftshift(X)));
xlabel('frequency (Hz)');
ylabel('real part');
title(['real part frequency-domain plot of ' inputFile]);
pause;
plot(f, imag(fftshift(X)));
xlabel('frequency (Hz)');
ylabel('imag part');
title(['imag part frequency-domain plot of ' inputFile]);
pause;
plot(f, abs(fftshift(X))); % linear amplitude by linear freq plot
xlabel('frequency (Hz)');
ylabel('amplitude');
title(['abs frequency-domain plot of ' inputFile]);
pause;
plot(f, 20*log10(abs(fftshift(X))+1.0e-10)); % dB by linear freq plot
xlabel('frequency (Hz)');
ylabel('amplitude (dB)');
title(['dB frequency-domain plot of ' inputFile]);
pause;
% display only positive frequency spectrum for log frequency scale
semilogx(f(numSamples/2+2:numSamples), 20*log10(abs(X(2:numSamples/2)))); % dB by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('amplitude (dB)');
title(['dB vs. log freq, frequency-domain plot of ' inputFile]);
pause;
semilogx(f(numSamples/2+2:numSamples), (180/pi)*angle(X(2:numSamples/2))); % phase by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('phase (degrees)');
title(['phase vs. log freq, frequency-domain plot of ' inputFile]);
pause;
%
% this is an alternate method of unwrapping phase
%
% phase = cumsum([angle(X(1)); angle( X(2:numSamples/2) ./ X(1:numSamples/2-1) ) ]);
% semilogx(f(numSamples/2+2:numSamples), phase(2:numSamples/2)); % unwrapped phase by log freq plot
%
semilogx(f(numSamples/2+2:numSamples), unwrap(angle(X(2:numSamples/2)))); % unwrapped phase by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('unwrapped phase (radians)');
title(['unwrapped phase vs. log freq, frequency-domain plot of ' inputFile]);
If you were windowing segments of audio and passing them to the FFT, then you should use fftshift() on the input to the FFT to define the center of the windowed segment as the t=0 point.
[x_input, Fs] = audioread('vibe.wav'); % load time-domain input
N = 2*floor(length(x_input)/2); % make sure N is even
x = x_input(1:N);
t = linspace(-N/2, N/2-1, N); % values of time in units of samples
omega = linspace(-pi, pi*(1-2/N), N); % values of (normalized) angular frequency
X = fftshift( fft( fftshift( x.*hamming(length(x)) ) ) );
[X_max k_max] = max( abs(X) );
figure(1);
plot(t, x, 'g');
figure(2);
plot(omega, abs(X), 'b');
hold on;
plot(omega(k_max), X_max, 'or');
hold off;

Frequency spectrum of signal in Matlab

Here is the code I use to plot a function in frequency domain in Matlab:
dt = 1/10000; % sampling rate
et = 0.1; % end of the interval
t = 0:dt:et; % sampling range
y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); % sample the signal
subplot(2,1,1); % first of two plots
plot(t,y); grid on % plot with grid
xlabel('Time (s)'); % time expressed in seconds
ylabel('Amplitude'); % amplitude as function of time
Y = fft(y); % compute Fourier transform
n = size(y,2)/2; % 2nd half are complex conjugates
amp_spec = abs(Y)/n; % absolute value and normalize
subplot(2,1,2); % second of two plots
freq = (0:100)/(2*n*dt); % abscissa viewing window
stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
ylabel('Amplitude'); % amplitude as function of frequency
The problem is, when I zoom in my graph I don't see peaks exactly on 50Hz, 90Hz and 180Hz.
What did I do wrong in my code?
The problem is that in order to get perfect spectra (peaks on 50,90,180 and 0 otherwise), your interval should be a multiplier of all the frequencies.
Explanation: consider y=sin(2.*pi.*t). Use your code to plot it with:
1) et = 1-dt;
2) et = 1;
In the first case, if you zoom in, you will see that peak is perfectly on 1 Hz. In the second case it is not (also very close).
Why? Because you are working with the finite number of points, and, consequently, finite number of frequencies. If 1 Hz is among this set of frequencies (1st case), you will get perfect peak at 1 Hz at zeros at all others frequencies.
In case 2 there is no 1 Hz frequency in your set, so you will get peak on the nearest frequencies (and also it will have finite width).
Ultimately, in your original code, there are no 50, 90, 180 Hz frequencies in your full set of frequencies.

Matlab Low Pass filter using fft

I implemented a simple low pass filter in matlab using a forward and backward fft.
It works in principle, but the minimum and maximum values differ from the original.
signal = data;
%% fourier spectrum
% number of elements in fft
NFFT = 1024;
% fft of data
Y = fft(signal,NFFT)/L;
% plot(freq_spectrum)
%% apply filter
fullw = zeros(1, numel(Y));
fullw( 1 : 20 ) = 1;
filteredData = Y.*fullw;
%% invers fft
iY = ifft(filteredData,NFFT);
% amplitude is in abs part
fY = abs(iY);
% use only the length of the original data
fY = fY(1:numel(signal));
filteredSignal = fY * NFFT; % correct maximum
clf; hold on;
plot(signal, 'g-')
plot(filteredSignal ,'b-')
hold off;
the resulting image looks like this
What am I doing wrong? If I normalize both data the filtered signal looks correct.
Just to remind ourselves of how MATLAB stores frequency content for Y = fft(y,N):
Y(1) is the constant offset
Y(2:N/2 + 1) is the set of positive frequencies
Y(N/2 + 2:end) is the set of negative frequencies... (normally we would plot this left of the vertical axis)
In order to make a true low pass filter, we must preserve both the low positive frequencies and the low negative frequencies.
Here's an example of doing this with a multiplicative rectangle filter in the frequency domain, as you've done:
% make our noisy function
t = linspace(1,10,1024);
x = -(t-5).^2 + 2;
y = awgn(x,0.5);
Y = fft(y,1024);
r = 20; % range of frequencies we want to preserve
rectangle = zeros(size(Y));
rectangle(1:r+1) = 1; % preserve low +ve frequencies
y_half = ifft(Y.*rectangle,1024); % +ve low-pass filtered signal
rectangle(end-r+1:end) = 1; % preserve low -ve frequencies
y_rect = ifft(Y.*rectangle,1024); % full low-pass filtered signal
hold on;
plot(t,y,'g--'); plot(t,x,'k','LineWidth',2); plot(t,y_half,'b','LineWidth',2); plot(t,y_rect,'r','LineWidth',2);
legend('noisy signal','true signal','+ve low-pass','full low-pass','Location','southwest')
The full low-pass fitler does a better job but you'll notice that the reconstruction is a bit "wavy". This is because multiplication with a rectangle function in the frequency domain is the same as a convolution with a sinc function in the time domain. Convolution with a sinc fucntion replaces every point with a very uneven weighted average of its neighbours, hence the "wave" effect.
A gaussian filter has nicer low-pass filter properties because the fourier transform of a gaussian is a gaussian. A gaussian decays to zero nicely so it doesn't include far-off neighbours in the weighted average during convolution. Here is an example with a gaussian filter preserving the positive and negative frequencies:
gauss = zeros(size(Y));
sigma = 8; % just a guess for a range of ~20
gauss(1:r+1) = exp(-(1:r+1).^ 2 / (2 * sigma ^ 2)); % +ve frequencies
gauss(end-r+1:end) = fliplr(gauss(2:r+1)); % -ve frequencies
y_gauss = ifft(Y.*gauss,1024);
hold on;
plot(t,x,'k','LineWidth',2); plot(t,y_rect,'r','LineWidth',2); plot(t,y_gauss,'c','LineWidth',2);
legend('true signal','full low-pass','gaussian','Location','southwest')
As you can see, the reconstruction is much better this way.

I did a step function FFT on matlab but only getting one side of the spectrum (0 to infinity)

with this code i am only getting half og the fft spectrum from 0 to positive infinity . i am trying to mirror this along the y axis to get the other half which is symmetric to this one from 0 to negative infinity.
Fs = 1000; %sampling rate
Ts = 1/Fs; %sampling time interval
t = -10:Ts:10-Ts; %sampling period
n = length(t); %number of samples
y = heaviside(t)-heaviside(t-4); %the step curve
matlabFFT = figure; %create a new figure
YfreqDomain = fft(y); %take the fft of our step funcion, y(t)
y=abs(YfreqDomain);
plot(y)
xlabel('Sample Number')
ylabel('Amplitude')
title('Using the Matlab fft command')
grid
axis([-100,100,0,5000])
That's normal behaviour. The FFT returns the spectrum in positive frequencies only (between 0 and Fs). You can use fftshift to correct that. The zero frequency will then be at the center of the x axis. So you should use
plot(fftshift(y))
axis([-100+1e4,100+1e4,0,5000])