

So let’s adjust out function above so that we only get the positive frequencies.Ī Custom Function for fft to Obtain only the Positive FrequenciesThe following function is a modification of the above function, and will help you plot only the positive frequencies of the spectrum. In general, the positive side of the spectrum is used, while the negative side is ignored. Thus, we only need one side of the spectrum. Redundant Information in the FFTAs you can see from the plots above, the information within the frequency spectrum is entirely symmetric. %remember to take the abs of YfreqDomain to get the magnitude!Īxis()Here’s what you should see:Īs you can see, this plot is basically identical to what we would expect! We get peaks at both -4 Hz and +4 Hz, and the amplitude of the peaks are 1. Now, copy and paste these commands into the Matlab command prompt. Let’s use the sine wave from above and do a quick example (Remember to set the Matlab directory to the location where you saved the previous m-file). It takes in as input the signal to be transformed, and the sampling rate. The function outputs the correct frequency range and the transformed signal. X=fftshift(X) %shifts the fft data so that it is centeredThis is a relatively simple function to use.
#Fft matlab code
%this part of the code generates that frequency axis if mod(N,2)=0įreq=k/T %the frequency axis %takes the fft of the signal, and adjusts the amplitude accordingly %this is a custom function that helps in plotting the two-sided spectrum %x is the signal that is to be transformed %Fs is the sampling rate Copy this code into an m-file and save it. The spectrum is not centered around zeroĪ Custom Function for fft to Obtain a Two-Sided SpectrumHere is a helpful function I learned at Harvey Mudd College which will simplify the process of plotting a two-sided spectrum.How can we tell that the peaks are in the right place? The x-axis gives us no information on the frequency.If you notice, there are a couple of things that are missing. This doesn’t quite look like what we predicted above. Y = 2*sin(2*pi*fo*t) %the sine curve %plot the cosine curve in the time domain Next, let’s generate this curve within matlab using the following commands:.Let’s start off with a simple cosine wave, written in the following manner:.
#Fft matlab how to
In addition, it will show you how to obtain a two-sided spectrum as well as a positive frequency spectrum for a given signal. The fft command only operates on the y-data (converting the y-data from the time domain into the frequency domain), so it’s up to the user to determine what the x-data in the frequency domain will be! This tutorial will show you how to define your x-axis so that your fft results are meaningful. When we represent a signal within matlab, we usually use two vectors, one for the x data, and one for the y data. The fft command is in itself pretty simple, but takes a little bit of getting used to in order to be used effectively. Introduction In this tutorial, we will discuss how to use the fft (Fast Fourier Transform) command within MATLAB.
