User defined FFT function parameter not recognized

Hi All
I am trying to use the FFT function explained in this link
but when I use a nx2 signal and a scalar value for ScanRate I get an error :
Shall someone tell why isnt the function getting my input value
Unrecognized function or variable 'ScanRate'.
% function [frq, amp, phase] = simpleFFT( signal, ScanRate)
% Purpose: perform an FFT of a real-valued input signal, and generate the single-sided
% output, in amplitude and phase, scaled to the same units as the input.
%inputs:
% signal: the signal to transform
% ScanRate: the sampling frequency (in Hertz)
% outputs:
% frq: a vector of frequency points (in Hertz)
% amp: a vector of amplitudes (same units as the input signal)
% phase: a vector of phases (in radians)
function [frq, amp, phase] = simpleFFT( signal, ScanRate)
n = length(signal);
z = fft(signal, n); %do the actual work
%generate the vector of frequencies
halfn = floor(n / 2)+1;
deltaf = 1 / ( n / ScanRate);
frq = (0:(halfn-1)) * deltaf;
% convert from 2 sided spectrum to 1 sided
%(assuming that the input is a real signal)
amp(1) = abs(z(1)) ./ (n);
amp(2:(halfn-1)) = abs(z(2:(halfn-1))) ./ (n / 2);
amp(halfn) = abs(z(halfn)) ./ (n);
phase = angle(z(1:halfn));

3 个评论

Show us how you instantiated the arguments that you passed in, and the line of code where you actually called the function with those variables.
You didn't just click the green Run triangle did you? Because that's a function where it won't run unless you pass in input arguments, which clicking the green run triangle won't do - it has no idea what you'd want for those variables.
it was a stupid wrong letter typo from me. sorry
when I do
plot( frq,amp)
the maximum value is not 1
applying on other signals with small values, around 1 to 4 mm , I get huge frf values of 180 mm or so
I think it has not mentioned how should be the time and amplitude be arranged in the signal matrix

请先登录,再进行评论。

回答(1 个)

How did you call the function? When you get an error, report the ENTIRE error message, everyting in red. This lets us know how you called the function.
Just because you have a variable named ScanRate in your workspace does not tell MATLAB to automatically pass it into your function.
If you want the function to see the variable, then you NEED to pass it in. Call it like this:
[frq, amp, phase] = simpleFFT( signal, ScanRate);
So you need ot pass in TWO arguments to your function. They can have any names as you pass them in, but inside the function, now ScanRate will be defined.
If you don't, well, then MATLAB will generate an error - the one you got.

5 个评论

sure , here is the error
Unrecognized function or variable 'ScanRate'.
Error in 2020(line 6999)
[frq, amp, phase] = simpleFFT(signal,ScanRate)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
ok it was a stupid wrong letter typo from me. sorry
now I tried the function, but I get a weird result from applying this FFT on my signal ,shall you try iy please
when I do
plot( frq,amp)
the maximum value is not 1
applying on other signals with small values, around 1 to 4 mm , I get huge frf values of 180 mm or so
I think it has not mentioned how should be the time and amplitude be arranged in the signal matrix

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Time-Frequency Analysis 的更多信息

提问:

2020-11-29

评论:

2020-11-29

Community Treasure Hunt

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

Start Hunting!

Translated by