code fft for matlab function block

5 views (last 30 days)
Rafaela Marchetti Martin
Rafaela Marchetti Martin on 26 May 2023
Commented: Walter Roberson on 26 May 2023
I'm developing an interferometer in Simulink and I wanted to calculate the FFT of the output signal using the "matlab function" block, but I'm not getting the code right, it generates several errors. And I wanted the function to read the frequency that comes from the signal and not have to add it to the code, but I couldn't get the code to run. Can anybody help me?
function [f, P1] = FFTsignal (S)
Fs = 20000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 80; % Length of signal
t = (0:L-1)*T; % Time vector
X = S + 2*randn(size(t));
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
end
  2 Comments
Steven Lord
Steven Lord on 26 May 2023
it generates several errors
Can you share the text of those error messages? That information may help determine the cause of those errors more quickly.
Rafaela Marchetti Martin
Rafaela Marchetti Martin on 26 May 2023
Thanks for listening. Follow the errors below.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 26 May 2023
Are you sure you intended to take the fft of the input argument S? From the error message that input is a scalar (or a length that's not compatible with your value of L, which you state is the length of the signal) but you're trying to index into more than just the first element of its FFT.
x = 42
x = 42
L = 80;
y = fft(x)
y = 42
y(1:L/2+1)
Index exceeds the number of array elements. Index must not exceed 1.
  2 Comments
Rafaela Marchetti Martin
Rafaela Marchetti Martin on 26 May 2023
I intend to calculate the FFT of the signal, but I don't know how to modify the code to calculate correctly and present the plot.
Walter Roberson
Walter Roberson on 26 May 2023
The center subsystem of the model is shown as having a signal size of 1. You add a random source that I cannot see the size of, split off the result and send it to your FFTsignal function.
There are two possibilities here:
  1. Your random source might be scalar. If so then the sum with the scalar signal would be scalar, and you would be sending a scalar to S. fft() of a scalar is not going to do anything useful; OR
  2. Your random source might be non-scalar. If so then the sum with the scalar would act as a constant shift on the fft characteristics of the random noise, which is unlikely to do anything useful for you.
If you want to be using fft on a group of samples then the group of samples has to reach the fft function, either because the signal is inherently non-scalar, or else because you inserted something like a buffer block to accmulate a number of consecutive signals to pass through the fft analysis.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!