MATLAB Answers

drawing a fast Fourier transform

14 views (last 30 days)
Ken Chi
Ken Chi on 10 Jan 2020
Edited: Ken Chi on 12 Jan 2020
how to draw fft? Hints wanted

Accepted Answer

Meg Noah
Meg Noah on 11 Jan 2020
Here's a solution
X = dlmread('ECG.csv');
Fs = 350*60; % [samples/min] sampling frequency
T = 1/Fs; % [s] sampling period
N = 3000; % [samples] Length of signal
t = (0:N-1)*T; % [s] Time vector
deltaF = Fs/N; % [1/min]) frequency intervalue of discrete signal
figure('color','white','position',[70 100 600 900]);
subplot(3,1,1);
plot(60*t,X)
title({'Heartbeat data'})
xlabel('t (seconds)')
ylabel('X(t)')
% compute the fast fourier transform
Y = fft(X);
% manually shifting the FFT
Y = abs(Y/N);
Amp = [Y(ceil(end/2)+1:end)' Y(1) Y(2:ceil(end/2))']';
if (mod(N,2) == 0)
sampleIndex = -N/2:1:N/2-1; %raw index for FFT plot
else
sampleIndex = -(N-1)/2:1:(N-1)/2; %raw index for FFT plot
end
AmpSquared = Amp.^2;
subplot(3,1,2);
plot(deltaF*sampleIndex, AmpSquared);
hold on;
idx = find(AmpSquared > 15);
idx(sampleIndex(idx) < 0) = [];
plot(deltaF*sampleIndex(idx), AmpSquared(idx), '+');
for k = 1:length(idx)
if (idx(k) > (N-1)/2 && AmpSquared(idx(k))> 15)
h = text(deltaF*sampleIndex(idx(k)), AmpSquared(idx(k))+2,...
['f=' num2str(deltaF*sampleIndex(idx(k))) ' BPM']);
set(h,'rotation',60)
end
end
xlabel('Frequency [BPM]');
ylabel('|FFT(ECG)|^2');
title(['Power of FFT of ECG']);
xlim([0 max(deltaF*sampleIndex)/4]);
subplot(3,1,3);
half_f = deltaF*(0:(N/2));
plot(60*fftshift([half_f -fliplr(half_f(2:end+mod(N,2)-1))]), ...
abs(fftshift(Y)/N).^2);
xlabel('Frequency [BPM]');
ylabel('|FFT(ECG)|^2');
title('Using fftshift - Displaying Full Spectrum Power');
HeartbeatPower.png
  1 Comment
Meg Noah
Meg Noah on 11 Jan 2020
You'll need to change the xlim values to get to 0 to 120 BPM.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by