Effacer les filtres
Effacer les filtres

Simulink and FFT function

2 vues (au cours des 30 derniers jours)
marcusbarnet
marcusbarnet le 31 Mar 2018
Commenté : marcusbarnet le 31 Mar 2018
Hi to all,
I hope to be able to find a solution for my problem.
I'm using Simulink to perform data analysis on accelerations along the Z-axis coming from a accelerometer at a sampling rate of 160 Hz. I would like to calculate the FFT over 1600 samples (so, every 10 seconds) during a real-time acquisition and so I'm using this diagram:
I would like to make the stats block output the four maximum peaks from the FFT. In Matlab, the code works very well since I'm able to obtain every time four maximum peaks and their frequencies, but when I use this same code in the stats blocks I get different results.
The code I use in Matlab is:
figure(2)
N = length(raw_accelearation(start*10:stop*10));
xdft = fft(raw_accelearation(start*10:stop*10));
xdft = xdft(1:floor(N/2+1));
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(raw_accelearation(start*10:stop*10)):Fs/2;
L = length(psdx);
windowed_signal=psdx.*hamming(L);
plot(freq,10*log10(windowed_signal))
grid on
title('Periodogram for asphalt')
xlabel('Frequency (Hz)')
ylabel('Power/Frequency (dB/Hz)')
[maxpeaks, idx] = findpeaks(10*log10(psdx),'MinPeakDistance',500,'NPeaks',4,'MinPeakHeight',-10)
peak1 = maxpeaks(1)
freq1 = freq(idx(1))
peak2 = maxpeaks(2)
freq2 = freq(idx(2))
peak3 = maxpeaks(3)
freq3 = freq(idx(3))
peak4 = maxpeaks(4)
freq4 = freq(idx(4))
and this is the plot:
This is the code I use in the stats block in Simulink:
function [x,y, z]= stats(u)
coder.extrinsic('findpeaks');
y=10;
x=10;
z=10;
Fs=160;
raw_acceleration = u;
N = length(raw_acceleration);
xdft = fft(raw_acceleration);
xdft = xdft(1:floor(N/2+1));
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(raw_acceleration):Fs/2;
% plot(freq,10*log10(psdx))
% grid on
% title('Periodogram for asphalt')
% xlabel('Frequency (Hz)')
% ylabel('Power/Frequency (dB/Hz)')
[maxpeaks, idx] = findpeaks(10*log10(psdx),'MinPeakDistance',300,'NPeaks',4,'MinPeakHeight',mean(10*log10(psdx)))
peak1 = maxpeaks(1)
% freq1 = freq(idx(1))
peak2 = maxpeaks(2)
% freq2 = freq(idx(2))
% peak3 = maxpeaks(3)
% freq3 = freq(idx(3))
% peak4 = maxpeaks(4)
% freq4 = freq(idx(4))
y = peak2
x = peak1
z = length(maxpeaks)
end
The problems in Simulink are:
  • I can't output all the four peaks because the code is able to find only 3 peaks
  • If I increase the MinPeakDistance I get an error: Index exceeds matrix dimensions.
How can I solve the problems?
This is how I setup the buffer block and the window block.
  1 commentaire
marcusbarnet
marcusbarnet le 31 Mar 2018
I found out that during the running time, it finds 2 peaks and sometimes 3 peaks..

Connectez-vous pour commenter.

Réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by