Hello, I'm building a ECG using Arduino MEGA 2560. I've already built the circuit and now I'm on the data processing phase.

I want to measure the BPM in real time, for that I'm taking data from the patient (let's call it like that) for 8 seconds and then using the FFT (from the library "arduinoFFT.h") to get the predominant frequency, hence the BPM. And I do that continuously, each time I calculate the FFT I start collecting new data for 8 seconds and so on, so that way I get the BPM "in real time".

I wanted to know if there is a way to remove the contribution from the first point of the Fourier transform to add that of the last point (voltage in my case) measured, so that my FFT is continuously updated and the calculation is faster.

/*
Heartbeat counter using FFT
Reads an analog input on pin 15 (heartbeat) which is stored inside the vReal array. The heartbeat
is windowed according to Hamming function. The FFT is computed using the windowed signal. Then the
magnitudes of each of the frequencies that compose the signal are calculated. Finally, the frequency
with the highest peak is obtained, being that the main frequency present in the signal, hence the BPM.
*/
#include "arduinoFFT.h"
arduinoFFT FFT = arduinoFFT(); /* Create FFT object */
const uint16_t samples = 256; /* This value MUST ALWAYS be a power of 2 */
double vReal[samples];
double vImag[samples];
double t[samples];
#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02
#define SCL_PLOT 0x03
void setup() {
Serial.begin(38400);
while(!Serial);
Serial.println("Ready");
}
void loop() {
/* Sampling */
for (uint16_t i = 0; i < samples; i++)
{
t[i] = micros(); /* Store the time */
//Serial.println(t[i]);
vReal[i] = analogRead(A15); /* Read the electrical signal of the heart */
vImag[i] = 0.0;
delay(31.25); /* Delay between readings of channel A15 */
}
const double samplingFrequency = (samples)/((t[samples - 1] - t[0])*pow(10, -6));
//Serial.println(samples);
//Serial.println((t[samples - 1] - t[0])*pow(10, -6));
//Serial.println(samplingFrequency);
FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); /* Weigh data */
FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
double x = FFT.MajorPeak(vReal, samples, samplingFrequency)*60;
Serial.print("BPM: ");
Serial.println(x, 6);
//delay(500); /* Repeat after delay */
}

I'm storing in an array the voltage I read from the electrodes, I'm using 256 samples, and then I'm calculating the FFT. I'm repeating that all the time so that I get the actualised BPM. The thing is that if I have to read and store all new 256 it takes more time. I wanted to know if there is a way in which I don't have to do that. I could just read the next voltage value and put it in my Fourier transform (to do that I would have to eliminate the first voltage value I used so that I still have 256 samples). I don't know if I'm making myself clear.

STFT is a well-known technique in signal processing to analyze non-stationary signals. STFT is segmenting the signal into narrow time intervals and takes the Fourier transform of each segment. In Dewesoft’s FFT setup you can set FFT’s resolution, Window, and Overlap and for better understanding what that means, let’s look at the picture below. Window size depends on FFT’s resolution, we can just say FFT size (representing a segment of a signal).