Clear Filters
Clear Filters

How do I resample to have a uniform number of samples for each cycle of my signal?

15 views (last 30 days)
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;
I have also attached the variable data for 'qrs_i_raw' and 'maxamp' on which the rest of the code is based.

Answers (1)

Mathieu NOE
Mathieu NOE on 22 Nov 2020
hello Cai
this little demo code for generating exactly 800 samples between each cycles
y = maxamp;
samples = length(y);
Fs = 1e3;
dt = 1/Fs;
time = (0:samples-1)*dt;
[b,a] = butter(1,0.05,'high');
y_detrend = filtfilt(b,a,y);
threshold = 0.4*max(y_detrend);
[ind_crossing,x_crossing,y_crossing]= crossing_V6(y_detrend,time,threshold,'linear');
% keep only first crossing event
ind = (1:2:length(ind_crossing));
ind_crossing = ind_crossing(ind);
x_crossing = x_crossing(ind);
y_crossing = y_crossing(ind);
offset_samples = 30; % acts a a pre trigger shift to capture the beginning of each cycle
for ci =1:length(ind_crossing)-1
ind = find(time>=x_crossing(ci) & time<=x_crossing(ci+1));
ind = ind - offset_samples;
time_extract = time(ind);
new_time = linspace(time_extract(1),time_extract(end),800);
new_data = interp1(time(ind),y(ind),new_time);
figure(2),plot(new_time,new_data);hold on
hold off

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!