code fft for matlab function block
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Rafaela Marchetti Martin
el 26 de Mayo de 2023
Comentada: Walter Roberson
el 26 de Mayo de 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 comentarios
Steven Lord
el 26 de Mayo de 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.
Respuestas (1)
Steven Lord
el 26 de Mayo de 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
L = 80;
y = fft(x)
y(1:L/2+1)
2 comentarios
Walter Roberson
el 26 de Mayo de 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:
- 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
- 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.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!