X = dlmread('ECG.csv');
Fs = 350*60;
T = 1/Fs;
N = 3000;
t = (0:N-1)*T;
deltaF = Fs/N;
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)')
Y = fft(X);
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;
else
sampleIndex = -(N-1)/2:1:(N-1)/2;
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');
Best Answer