How to use 'resample' function?

조회 수: 12 (최근 30일)
hollyhue
hollyhue 2021년 1월 4일
댓글: hollyhue 2021년 1월 8일
I have a set of data (response vs. time) with 2000+ data points, collected over the course of 500 seconds. I would like to reduce the number of data points to 100 by using only data points collected every 5 seconds, but my sampling frequency does not provide a data point at 5 second intervals. After searching MATLAB Answers, it appears many people use the resample function. I have several questions:
  1. What is the difference between using the resample function and linear interpolation?
  2. I do not understand what the upsampling (p) and downsampling (q) factors are. If I set p=1 and q=1, will I just be linear interpolating? Are p and q important to reducing edge effects? I have found that if I set p and q to 1, there are no edge effects, why is this? See example below.
I have attempted to implement the resample function as follows with padded data at the beginning and end as suggested by this MATLAB Answer:
fs=1/5;
x=time; % time vector in sec
y=response; % change in response vector
xpad = [repmat(x(1), 1, 20)'; x; repmat(x(end), 1, 20)'];
tpad = [repmat(y(1), 1, 20)'; y; repmat(y(end), 1, 20)'];
[yB,xB]=resample(tpad,xpad,fs1,1,1,'linear'); %p=1, q=1
  댓글 수: 7
Adam Danz
Adam Danz 2021년 1월 6일
편집: Adam Danz 2021년 1월 6일
I just dealt with this problem a few days ago. I was analyzing data collected at 150Hz which is 6.667ms intervals but since the compute stored the data at ms resolution, the intervals were either 6 or 7 ms. I preferred to work with the raw data rather than interpolated data.
If your sampling frequency variation is not very large and if you have the sample timestamps you can differentiate that timestamps to get the sampling interval for each sample. Then you could subsample your data based on the cumulative intervals which will result in approximately equal segments of time.
For example,
timestamps = [6 13 20 26 32 38 45 52 59 66 73 79 86 92 99 106 112 118 124 131 138 145 151 158 164 171 178 184 191 198 204 210 216 222 229 236]; %ms or some other unit of time
data = 1:numel(timestamps); % sampled data (arbitrary)
dt = diff([0,timestamps]); % sampling intervals (ms)
desiredInterval = 20; % Desired sub-sampling rate (ms)
idx = find(diff([-inf, mod(cumsum(dt), desiredInterval)]) <=0) +1; % index of subsampled values
subsampledData = data(idx); % subsampled data
diff(timestamps(idx)) % the actual subsampled intervals which should be close to desiredInterval if your sampling freq variation is not too high
ans = 1×10
26 21 19 20 19 20 20 20 19 19
hollyhue
hollyhue 2021년 1월 8일
I would also like to use the raw data rather than interpolation, but I need exactly the same time interval lengths. This method gives me slightly different interval length, albeit very close to each other. The resampling function gives me exactly the right interval lengths, but I cannot figure out why p=1 and q=1 are the ONLY upsampling and downsampling factors that do not cause edge effects. Does anyone know why? Or how to verify these factors?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by