Help with m file

1 view (last 30 days)
Klimis
Klimis on 6 Dec 2014
Commented: Star Strider on 6 Dec 2014
I am having a problem on the creation of my m file.
When i call my m file, i pass the parameters T and N, and then i am trying to put my signal, which is a variable of time(t). Matlab says that variable (t) is Undefined even though in my m file i calculated the time :
t= (0:Ts:(N-1)*Ts);
What is wrong with that?
My m file is here :
function [x,X_mag] = frequencyfft(T,N,signal)
Ts=T/N;
Fs=1/Ts;
df=Fs/N;
t= (0:Ts:(N-1)*Ts);
x=signal
subplot(2,1,1)
plot(t,x);
X=fft(x,N);
fourier_X=X/Fs;
X_mag=abs(fourier_X);
f=(0:df:(N-1)*df)-N/2*df;
subplot(2,1,2);
stem(f,fftshift(X_mag));
Thanks in advance
  9 Comments
Klimis
Klimis on 6 Dec 2014
Image Analyst the thing is that the length of the signal is determined from its time. Thats why i have to determine time(t) first and then the signal will have the same length as time(t).
Star Strider i dont get are you trying to show
Image Analyst
Image Analyst on 6 Dec 2014
In short, run your program and when it errors out, copy everything you see in red, and paste it back here . Make sense?

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 6 Dec 2014
The error implies that you apparently did not define ‘t’ in your script that calls frequencyfft, becasue it is attempting to evaluate sin(2*pi*t/T) before it passes it as an argument to frequencyfft.
The error does not appear to be in frequencyfft itself.
  5 Comments
Star Strider
Star Strider on 6 Dec 2014
Not really. You are creating the signal you want frequencyfft to analyse.
Do you want to pass a function handle to frequencyfft in your ‘signal’ argument? You can do that by passing it as for instance @sin for the sine function. Then this line:
x=signal
becomes:
x = signal(t);
Run this little function to see how it works:
function y = trigeval(fun)
t = linspace(0,2*pi);
y = fun(t);
end
y = trigeval(@tan)
figure(1)
plot(y)
Star Strider
Star Strider on 6 Dec 2014
One question I should have asked hours ago:
What do you want frequencyfft to do?
What input arguments do you want to pass to it, and what do you want it to return?

Sign in to comment.


Image Analyst
Image Analyst on 6 Dec 2014
You did exactly what John suspected. You did this:
>> [x,X_mag] = frequencyfft(0.1,128,sin(2*pi*t/T))
Undefined function or variable 't'.
Now, when you call that, what do you think it's supposed to use for t? You haven't defined it. You need to define t first, and then create your signal and pass it in
t= (0:Ts:(N-1)*Ts); % This is what you were missing when you tried to call it.
signal = sin(2*pi*t/T);
[x,X_mag] = frequencyfft(0.1,128,signal)
  1 Comment
Klimis
Klimis on 6 Dec 2014
Ts is undefined also. That means i have to define T and N before i use my m file so i can define my Ts and then my t, if thats the case i am doing all the work without my m file. Or maybe i am missing something from what you answered me?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!