I need to preprocess raw ecg data in R, here is a sample already standardized. I'm not an expert in signal processing nor experienced in working with medical data,...
Not being an expert on how the heart works and how its phases manifest themselves on the ElectroCardioGram (ECG) is not a problem. But it would help immensely if you mentioned what is the purpose of this, i.e what do you want to do, what are your specifications and constraints?
For example "We put the driver in the car, they go through the track a few times and then we want to know what was their heartbeat at a specific point in the track".
Excellent, then, do not even use ElectroCardioGraphy (ECG) because:
The electrical environment of the racing car is incredibly noisy already. For comparison, you are trying to measure a signal that has a max amplitude of a few mV through long leads that have to run from the chest of the driver to the ECG box. Just a short distance away from this long electrical loop, you have sparks going on and off, relays turning big loads on and off, electrical motors or stepper motors turning. All of these things create fields that the loop will pick up and they will contaminate your signal.
ECG measures the electrical activity of the heart via contact electrodes. Which means that the skin of the driver would have to be prepared, ideally shaved, cleaned with alcohol, then have a conductive gel applied to it and finally apply the electrodes and fix them with medical tape. Throughout the track, I would not expect the driver to pay attention to the electrodes or, to not sweat. Even in controlled conditions, if you tap the electrodes with your hand, you can observe changes in the signal.
Instead, use Pulse Oximetry. This is the way that most gadgets (e.g. smart watches, fitness wrist bands, etc) obtain Beats per Minute from the heart today. Pulse oximetry needs to have contact with the skin, but not because it measures electricity. Very briefly: It measures the attenuation of light that is proportional to the blood flow below it. Because the heart is pumping blood in a rythmic way, the flow is rythmic as well and this signal can be used to derive a relatively accurate BPM count.
It will also be much easier to cross reference this with the rest of the telemetry because it will simply be a time series of BPM measurements. Pulse oxymeters for people to manage their heart conditions are more accurate than gadget type instruments. But then again, they might get a bit of getting used to by the driver. (For an example, see this)
Below is a plot of how the data looks like,
No way.
Here (and here ) is what typical ECG signals look like.
To extract BPM, you need to "detect" those spikes which correspond to heart contractions and then measure the timing between them (also known as R-R interval, because the main spike of the ECG is the R peak).
Very briefly: QRS Detectors work by Pre-filtering -> Rectification -> Integration -> Thresholding -> QRS Complex pulse sequence -> R interval extraction -> Beat Detection. For much more detailed information about this, have a look at Moody's work.
What is depicted in the plot is saturation and possibly lots of noise from the contact. There is no trace of "typical" ECG complexes to work with.
Question 1) What strategy is needed in terms of subsampling, filtering to preprocess the data to clean get a clean ECG signal (google how it should look).
Why subsample? The signal is already in a bad state, why make it even worse? You could try a bandpass filter between 0.1 - 100Hz. This would reduce DC and cut out any high frequency noise but you have a lot of spikes which means that most of time times you will be "hearing" the ringing of the filter.
Question 2) Would this strategy work for EMG data also?
EMG is even worse because it picks up signals at higher frequencies. What is the application?
If you are trying to see the timing of the driver as to WHEN do they start to accelerate (i.e. leg muscles start working) versus when the car actually accelerates or brakes (i.e. break signal from telemetry), then maybe you can get away with very simple processing using thresholds. But if you are trying to make more fine measurements, you might be back into the same problem of lots of noise from various sources. In the case of EMG you will have to work around those, which means, twisted pair wires, shielding, as short lengths as possible, very good contact with the driver's skin, very good preparation of the driver's skin, perhaps even slightly different electrodes that attach on to the skin rather than simply be in contact with it.
Hope this helps.