Search code examples
matlabsignals

How to resample data input signal for a different number of samples?


I am using MATLAB R2020a on a MacOS. I have an ECG signal with a sampling frequency of 1kHz. From cycle to cycle, there are a different number of samples since the cycle lenghts are different. However, I would like to have an array which has an equal number of samples (around 800) for each cycle such that those 800 sample points are automatically fitted to the original sample points regardless of the number of original sample points. I know that the resample function allows resampling at a fraction of the frequency of the input signal, but I am not sure how this would help me achieve my aim given that I would like to resample for a fixed number of points. I would very much appreciate any suggestions. Thanks in advance

Here is my code:

% Delimit cycles in original maximum amplitude signal using indices from
% Pan Tompkins algorithm
number_cycles = round(length(qrs_i_raw)/2);
number_samples = 900;
cycle_points_maxamp = zeros(number_cycles, length(number_samples));

x_eachcycle = zeros(number_cycles, length(number_samples));
values_x = zeros(length(number_samples), 1);

y_eachcycle = zeros(number_cycles, length(number_samples));
values_y = zeros(length(number_samples), 1);

z_eachcycle = zeros(number_cycles, length(number_samples));
values_z = zeros(length(number_samples), 1);

v_eachcycle = zeros(number_cycles, length(number_samples));

w_eachcycle = zeros(number_cycles, length(number_samples));

for currentcycle = 1:length(number_cycles)
    
    values_maxamp = maxamp(qrs_i_raw(currentcycle):qrs_i_raw(currentcycle + 1)); % need to resample to only generate 900 samples 
    cycle_points_maxamp(currentcycle, 1:length(values_maxamp)) = values_maxamp;
    
    values_z(1 + 2*tau_milli_rounded(currentcycle):end) = cycle_points_maxamp(currentcycle, 1:end - 2*tau_milli_rounded(currentcycle));
    z_eachcycle(currentcycle, 1:length(values_z)) = values_z;
    
    values_y(1 + tau_milli_rounded(currentcycle):end) = cycle_points_maxamp(currentcycle, 1:end - tau_milli_rounded(currentcycle));
    y_eachcycle(currentcycle, 1:length(values_y)) = values_y;
    
    values_x(1:end) = cycle_points_maxamp(currentcycle, 1:end);
    x_eachcycle(currentcycle, 1:length(values_x)) = values_x;
    
    values_v = ((1/sqrt(6))*(x_eachcycle(currentcycle, 1:length(values_x))) + (y_eachcycle(currentcycle, 1:length(values_y))) - 2*(z_eachcycle(currentcycle, 1:length(values_z))));
    v_eachcycle(currentcycle, 1:length(values_v)) = values_v;
    
    values_w = ((1/sqrt(2))*(x_eachcycle(currentcycle, 1:length(values_x))) - (y_eachcycle(currentcycle, 1:length(values_y))));
    w_eachcycle(currentcycle, 1:length(values_w)) = values_w;
    
end

Here are the relevant variables: maxamp qrs_i_raw

cyclepointsarray


Solution

  • To resample a single cycle of the signal to have a length of 800 points/samples the resample() function can be used with parameters 800 and length(Random_Cycle). Here I used a sinc signal arbitrarily. The resample() function will interpolate (upsample) or decimate (downsample) according to the number of available sample points in the original signal. I would first partition your signal into separate cycles apply this type of resampling the concatenate the resampled components to generate your full resampled ECG signal.

    Resampled Signal

    Random_Cycle = sinc(-2*pi:0.5:2*pi);
    subplot(1,2,1); stem(Random_Cycle);
    title("Number of Samples: " + num2str(length(Random_Cycle)));
    
    Resampled_Signal = resample(Random_Cycle,800,length(Random_Cycle));
    subplot(1,2,2); stem(Resampled_Signal);
    title("Number of Samples: " + num2str(length(Resampled_Signal)));
    

    Ran using MATLAB R2019b