
How is filter delay removed from resample output
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
According to the help page for resample, MATLAB removes the filter delay from the output. I want to model the calculation of this delay and its removal. In the code below, I resample a signal (x1) by a factor of I/D = 128/75 in two ways. First, I use the built-in resample function to produce y1. Second, I upsample the signal by I, filter it (using the filter generated by resample function) and then downsample the resultant signal by D to produce y2.
% perform resampling manually
I = 128;
D = 75;
dataLen = 2000;
% Generate an input signal
f1 = 4000;
fsin = 64 * 1000;
%dataLen = 30;
x1 = exp(j*2*pi*f1/fsin*(0:dataLen-1)) + ...
exp(j*2*pi*1.56*f1/fsin*(0:dataLen-1)) + ...
exp(j*2*pi*2.05*f1/fsin*(0:dataLen-1));
%x1 = randn(1, dataLen);
%%Case 1: Resample
[y1, h1] = resample(x1, I, D);
%%case 2: upfirdn
x1up = upsample(x1, I);
x1filt = filter(h1, 1, x1up);
y2 = downsample(x1filt, D);
%%compensate for the delay
offset = 17;
yy2 = y2(offset+1 : end);
yy1 = y1(1:end-(offset));
%%comparison
err = sum(abs(yy1 - yy2))/sum(abs(yy2));
figure(1)
plot(real(yy1), 'b.-');
hold on; grid on;
plot(real(yy2), 'r.-');
ylabel('Output (real part)');
figure(2)
plot(imag(yy1), 'b.-');
hold on; grid on;
plot(imag(yy2), 'r.-');
ylabel('Output (imag part)');
The two outputs match very well except that y2 has a delay of about 17 samples on the front side. I have attached the comparison of the two outputs with and without removing the offset.


My question is: how does MATLAB get rid of the filter delay? If I simply chop-off the first 17 samples, then the length of my signal is reduced. How does MATLAB's output retain the correct output length (i.e., input length * I / D) while getting rid of the filter delay?
Thanks
1 comentario
Zach Boylan
el 8 de En. de 2024
Hey, I know this is old, but if ever someone bups into the resample delay trouble, here is what I figured out :
When resampling, the filter modifes the data and brings a delay.
As you might see in the plot, the blue line is a downsampling done by averaging the samples between two new sample rates, but the yellow and red lines are the results of resample function.
The red line is the output signal of resample advanced by one sample, and the yellow line is the output signal of resample.
Because I wanted the resampled signal to match the blue line (mean downsampled signal), I wasn't satified and I tried to mean the red and yellow results.
The new plot (purple, is much closer to what was the expected output.
So in case you want to properly correct for the delay, mean the output of resample with itself advanced by one sample :
```
figure;plot(data2);hold on;plot(data3(2:end));plot(data3);plot(mean([data3(2:end),data3(1:end-1)],2))
```

Respuestas (1)
Star Strider
el 29 de Jun. de 2016
The filter function introduces a phase delay that is the normal consequence of filtering. I have not looked at the resample code, but I suspect it uses the filtfilt function, that has a maximally flat phase response (no phase distortion or delay). Use it instead of filter and you will likely get the same result resample does.
0 comentarios
Ver también
Categorías
Más información sobre Multirate Signal Processing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!