How to find indices of resampled values from the indices of the original values?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have an ECG signal and indices of peaks locations of the signal.
I want to upsample the signal using "resample".
Then, how can I find the indices of the peaks from the resampleld ECG signal?
So, basically, I want to exactly associate the (index, peak) to (new index, new peaks) between two signals without using methods for peak detection.
Below is the description of the attached mat files.
s: 1D original ECG signal with the sampling frequency of 125 Hz,
snew: 1D upsampled ECG signal with the sampling frequency of 360 Hz, using "snew = resample(s,fsnew,fs)"
fs: sampling frequency of "s", i.e., 125 Hz,
fsnew: fs: sampling frequency of "snew", i.e., 360 Hz
pk: indices of peaks of original ECG signal (s).
0 Commenti
Risposte (1)
David Goodmanson
il 26 Ott 2024
Modificato: David Goodmanson
il 27 Ott 2024
Hello Mibang,
Rather than
snew = resample(s,fsnew,fs)
I think it's safer to use
[snew tnew] = resample(s,t,fsnew)
because the tnew array has useful information. The two methods give slightly different results for snew because the length of the two snew arrays differ by 1. But that difference seems fairly minor. You do have to supply t1, the original time array.
In what follows I used s --> s1 and snew --> s2.
The code starts by finding the nearest index in the new s2 array (call it ind2) that corresponds to the index of the peak in the s1 array. But quite often the actual peak in the s2 array is not at ind2 but rather at a very nearby point. I assume you want the indices of the actual peaks in s2. The code takes a few points about eiher side of ind2, set by the parameter indwidth (I use indwidth = 2), finds the local peak in that small interval and uses the resulting index to be ind2.
The demo uses a random signal s1, picks a few sample peaks from that and then finds the corresponding indices in s2.
f1 = 125;
f2 = 360;
% create time and signal arrays
n1 = 118;
t1 = (2 + (1:n1))/f1; % 3/f1 is an arbitrary starting point in time
s1 = rand(size(t1));
% find peaks, pick a few peak indices for demo purposes
% (lots of peaks since signal is random)
[~, ind1] = findpeaks(s1);
nind1 = length(ind1);
ind1 = ind1(1:nind1/6:nind1)
% resample
[s2 t2] = resample(s1,t1,f2);
n2 = length(t2);
t1start = t1(1);
t2start = t2(1);
% solve [t1start + (ind1-1)/f1 = t2start + (ind2-1)/f2] for ind2
% to find approximate indices for peaks in resampled array
ind2approx = round((t1start-t2start)*f2 +(f2/f1)*(ind1-1) +1)
% create a few indices around approximate indices
indwid = 2;
ind2near = ind2approx + (-indwid:indwid)'
ind2near(ind2near<1) = 1;
ind2near(ind2near>n2) = n2;
s2near = s2(ind2near);
% find peak indices for resampled array
[~, indrel] = max(s2near)
ind2 = ind2near(1,:) + indrel - 1 % the result
figure(1); grid on
plot(t1,s1,t2,s2,t1(ind1),s1(ind1),'ok',t2(ind2),s2(ind2),'o')
ind1 =
2 20 40 58 83 103
ind2approx =
4 56 113 165 237 295
ind2near =
2 54 111 163 235 293
3 55 112 164 236 294
4 56 113 165 237 295 % <-- cols are +-2 on either side of these values
5 57 114 166 238 296
6 58 115 167 239 297
indrel =
2 4 3 3 2 2
ind2 =
3 57 113 165 236 294
2 Commenti
David Goodmanson
il 26 Ott 2024
Modificato: David Goodmanson
il 26 Ott 2024
At this point you can try it on your data, using n1 = 3750 and whatever your peak indices ind1 are. As far as a window of 5, that's a judgment call, depending on how far apart the peaks are (including the peaks not picked by findpeak). Too wide, you might get the wrong peak (although it works if the peak you want is always higher than anything else around). Too narrow, you might miss the peak you want. I would say give it a try, do a plot for sure, zoom in and and see what it looks like.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!