Using a function in Matlab

168 Views Asked by At

Very new to MATLAB and Im trying to use the FFT function. I got a video which showed me that a function and a normal m file is needed. Created that but now dont know how to call the function from the m file. Here's is the function:

function [ X,freq ] = positiveFFT( x,Fs ) %This function takes data,and discards negatives only taking the %positives,getting rid of complex values %adopted from blinkdagger.com
N=length(x);
k=0:N-1;
T=N/Fs;
freq=k/T; %create the frequency range
X=fft(x)/N; %normalise the data
cutOff=ceil(N/2); %cut off frequency
X=X(1:cutOff);
freq=freq(1:cutOff);

end


and the m-file:
fo=4; %freqency of the sine wave(4cycles,4Hz)
Fs=100; %sampling rate
Ts=1/Fs; %sampling time interval
t=0:Ts:1-Ts;
n=length(t); %number of samples
y=2*sin(2*pi*fo*t);
[YfreqD,freqRng]=positiveFFT(y,Fs);
stem(freqRng,abs(YFreqD));

2

There are 2 best solutions below

2
On BEST ANSWER

I think you should change

stem(freqRng,abs(YFreqD));

as

stem(freqRng,abs(YfreqD));
0
On

@manny264

It looks like you ARE calling the function positiveFFT from within the m-file. the positiveFFT function in turn is calling the fft function of matlab.

[YfreqD,freqRng]=positiveFFT(y,Fs);

it is calling the function from within the "main". Not clear what else you expect it to do.