PPG_Delattre_Dey/FFT_heart_rate.m

66 lines
1.7 KiB
Matlab

%%%%%%%%%%%%%%%%%%%%%
% Script task: Normalize RGB data and plot FFT using the power spectra
%
% Input : RGB_data.csv -> average RGB values of each image
%
% Output : Fast Fourier Transform of X(t): a graph representing the Single-Sided Amplitude Spectrum of X(t)
%
% Author: Loic Delattre and Maryne DEY (maryne.dey@ecam.fr, loic.delattre@ecam.fr)
% Date: 07/02/2023
%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
%To be able to extract from external format (excel)
pkg load io
%Normalization of the data
data = frames_RGBs ()';
standard_deviation = std(data);
mean_value = mean(data);
for i = 1:size(data,1)
normalized_data_G(i,1) = (data(i,2)-mean_value(2))/standard_deviation(2); %%2 and not 1 because green is the 2nd color
endfor
%Input characteristics
img_num = length(data);
Fs = img_num/32; % Sampling frequency = X images in 32 seconds
T = 1/Fs; % Sampling period
L = img_num; % Length of signal = 32 seconds
t = (0:L-1)*T; % Time vector
X = normalized_data_G;
%Plot of the RGB data in the time domain
plot(t(1:L),X(1:L))
title("Signal")
xlabel("t (milliseconds)")
ylabel("X(t)")
%Finding power spectra P2 and P1
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;
% Finding boudaries
start = find(f==0.75);
stop = find(f==4);
P1_graph = P1(start:stop);
f_graph = f(start:stop);
%Graph of FFT in function of the frequency
plot(f_graph,P1_graph)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
%Extracting the heart rate
[heart_rate_Hz, index] = max(P1_graph);
corresponding_frequency = f_graph(index);
heart_rate_bpm = corresponding_frequency*60;
fprintf("Your heart rate is about %d bpm.\n", heart_rate_bpm)