how to create model in Simulink of MATLAB code? i was stuck in create FFT in Simulink and plotting frequency domain in Simulink.

조회 수: 1 (최근 30일)
below is matlab code which working properly in matlab to genrate FFT and plotting graph. but i am stuck in Simulink to create the model of below matlab code.
% Load data (simple numeric CSV).
data = readmatrix('C:\Users\v\Documents\MATLAB\testing\cable test\04042025\smallsensor.csv');
% Time domain
%num_samples = length(data);
time_max = 1.6; % Or your actual time duration
time = linspace(0, time_max, 4095)
figure;
plot(time, data);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time Domain Signal');
xlim ([0 1.6]);
grid on;
% Frequency domain (FFT) - Accurate
Fs = num_samples / time_max; % Sampling frequency
yf = fft(data);
% Positive frequencies (handles even/odd num_samples correctly)
if mod(num_samples, 2) == 0
f = Fs*(0:num_samples/2)/num_samples;
yf_positive = yf(1:num_samples/2+1);
else
f = Fs*(0:(num_samples-1)/2)/num_samples;
yf_positive = yf(1:(num_samples+1)/2);
end
yf_magnitude = abs(yf_positive);
figure;
plot(f, yf_magnitude);
xlabel('Frequency (Hz)');
ylabel('Magnitude (mm/s^2)');
title('FDS-Ma-3000-1.4mm (FFT)');
xlim([0 250]); % Set x-axis limits from 0 to 100 Hz (example)
%ylim([0 2500]); % Set y-axis limits from 0 to 500 (example)
grid on;

답변 (1개)

Nithin
Nithin 2025년 4월 21일
You can directly use a "MATLAB Function" block where you can encompass the FFT logic into a function and plot the data using the "scope" block.
Alternatively, if you are looking to build a model using the logic then refer to the steps below:
  • Since your data is stored in a CSV file, first load it into the MATLAB workspace so Simulink can access it.
  • If your data points are sampled at regular intervals, create a corresponding time vector in the workspace.
  • Use a "From Workspace" block in Simulink, setting its data input to "[time, data]" so it reads your time series correctly.
  • Insert an "FFT" block (from the DSP System Toolbox) and connect it to the output of the "From Workspace" block. Set the FFT length to match the number of samples in your data.
  • Next, add a "Complex to Magnitude-Angle" block and connect it to the FFT output, using the magnitude output for further processing.
  • To generate the frequency axis for plotting, use a MATLAB Function block with logic:
function f = freq_axis(N, Fs)
% N: FFT length, Fs: Sampling frequency
if mod(N,2) == 0
f = Fs*(0:N/2)/N;
else
f = Fs*(0:(N-1)/2)/N;
end
end
  • Use a "Selector" block to extract the first "N/2+1" elements from the magnitude output, representing the positive frequency components.
  • For visualization, use a "Scope" block for the time-domain signal, and an "XY Graph" block (from Simulink Extras) to plot the frequency vector against the FFT magnitude.
Refer to the following MathWorks documentations for more information:

카테고리

Help CenterFile Exchange에서 Transforms에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by