简体   繁体   中英

MATLAB: How do you avoid misaligment when resampling a segmented signal?

Due to memory limitations, I am segmenting and resampling my data before processing. However, in the process of resampling, somehow the samples get misaligned, ie if I have a signal A with length N which I divide into two smaller segments B and C, where B=A(1:j) and C=A(j+1:N), the part of the signal after j is misaligned compared to the original signal A.

The below code and images illustrate my problem. I'm not so converned about getting differences at the edges of the segments but the accumulation of error is problematic for me.

target_freq = 1000;
sampling_frequency = 32556;

order = 2;
fcutlow_low  = 140;
fs = sampling_frequency; 
[b_low,a_low] = butter(order,fcutlow_low/(fs/2), 'low');


myresamp = @(array) resample(array,target_freq,sampling_frequency);
myfilt = @(array) filtfilt(b_low, a_low, abs(array));

mydata = rand(1,100000);
target_freq = 1000;
original_freq = fs;

segresample = [myresamp(mydata(1:end/4)) myresamp(mydata((end/4)+1:end/2)) myresamp(mydata((end/2)+1:end))];

%% misalignment at the point of segmentation
figure
subplot(2,1,1)
plot(myresamp(mydata) - segresample)
ylabel('difference')
title('resampled')

%% this works as expected

segresample = [mydata(1:end/2)...
    mydata(end/2+1:end)];

subplot(2,1,2)
plot(mydata-segresample)
title('not resampled')
ylabel('difference')

在此输入图像描述

Resampling, I believe, is implemented with a set of FIR filters, so when you split up the signal in this manner, the resampling filters do not have any history of the input signal at your segmentation points, essentially the sum for the last n input samples is zero.

To avoid this, you need to segment your signal into frames that overlap, providing enough samples such that the FIR filter has enough input signal history. Consider:

|--------------------- long signal ------------------|
|----- frame 1 -----|
               |----- frame 2 -----|
                              |----- frame 3 -----|
                                             |----- frame 4 -----|

Resample each frame, then throw away the beginning of the resampled internal frames:

|----- frame 1 -----|
                    | frame 2 -----|
                                   | frame 3 -----|
                                                  |---|

By throwing away the beginning of the resampled internal frames, you are throwing away FIR output samples that did not have complete input signal history.

I'm not sure how to precompute the FIR delay size to use for the overlap size, so you'll have to experiment. It should be related to the ratio of sr and target_sr. Make the overlap size a parameter in your function so you can easily adjust it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM