Change of frequency content after using the retime function

2 views (last 30 days)
Hi everyone,
I am using the retime function to downsample a set of data. I was surprised when I saw that after using the function, the frequency content of the data has changed. It doesn't do that if I filter the data before retiming:
Could someone explain this phenomena to me?
Thanks!
I include the code below:
dt1 = seconds(test_table.time(2) - test_table.time(1));
dt2 = 0.01;
dt = seconds(dt2);
test_table_rt = retime(test_table,'regular','linear','TimeStep',dt);
%% Try after filtering
test_table_f = lowpass(test_table, 10); % Filter down to 10 Hz
test_table_frt = retime(test_table_f,'regular','linear','TimeStep',dt);
%% Plot the differences:
figure
plot(test_table.time, test_table.data)
hold on
plot(test_table_rt.time, test_table_rt.data)
plot(test_table_frt.time, test_table_frt.data)
%% Plot the different spectra
figure
ax = gca;
window = 1000;
[pxx,f] = pwelch(test_table.data,window*dt2/dt1,[],[],1/dt1);
plot(f,pxx, 'DisplayName', 'Original');
hold on
[pxx,f] = pwelch(test_table_rt.data,window,[],[],1/dt2);
plot(f,pxx, 'DisplayName', 'Retimed');
[pxx,f] = pwelch(test_table_frt.data,window,[],[],1/dt2);
plot(f,pxx, 'DisplayName', 'Filtered and retimed');
ax.XScale = 'log';
% ax.YScale = 'log';
grid on
grid minor
legend
xlabel('Frequency, Hz')
ylabel('PSD, dB/Hz')
xlim([1 50])

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 17 Nov 2023
Here is how to solve this issue:
load('question_data.mat')
dt1 = seconds(test_table.time(2) - test_table.time(1));
dt2 = 0.01;
dt = seconds(dt2);
% Averaging is necessary here before resampling
test_table_rt0 = retime(test_table,'secondly','mean');
% Resampling is done after averaging
test_table_rt = retime(test_table_rt0,'regular','linear','SampleRate',100);
% Try after filtering
test_table_f = lowpass(test_table, 10); % Filter down to 10 Hz
test_table_frt = retime(test_table_f,'regular','linear','TimeStep',dt);
%% Plot the differences:
figure
plot(test_table.time, test_table.data)
hold on
plot(test_table_rt.time, test_table_rt.data)
plot(test_table_frt.time, test_table_frt.data)
%% Plot the different spectra
figure
ax = gca;
window = 1000;
[pxx,f] = pwelch(test_table.data,window*dt2/dt1,[],[],1/dt1);
plot(f,pxx, 'DisplayName', 'Original');
hold on
[pxx,f] = pwelch(test_table_rt.data,window,[],[],1/dt2);
plot(f,pxx, 'DisplayName', 'Retimed');
[pxx,f] = pwelch(test_table_frt.data,window,[],[],1/dt2);
plot(f,pxx, 'DisplayName', 'Filtered and retimed');
ax.XScale = 'log';
% ax.YScale = 'log';
grid on
grid minor
legend
xlabel('Frequency, Hz')
ylabel('PSD, dB/Hz')
xlim([1 50])

Peter Perkins
Peter Perkins on 17 Nov 2023
Julius, I'm definitely not any kind of signal processing expert, but I think what you want is resample, from the Signal Processing Tbx.
If I replace this
%test_table_f = lowpass(test_table, 10); % Filter down to 10 Hz
%test_table_frt = retime(test_table_f,'regular','linear','TimeStep',dt);
in your code with this
test_table_frt = resample(test_table,1,10);
test_table_frt.Properties.DimensionNames(1) = "time"; % just cosmetics to make code below this work
I get this:
  1 Comment
Peter Perkins
Peter Perkins on 17 Nov 2023
Edited: Peter Perkins on 17 Nov 2023
It is perhaps not clear enough in the doc for retime, but this
dt1 = <ms time step, so .001>
dt2 = 0.01;
dt = seconds(dt2);
test_table_rt = retime(test_table,'regular','linear','TimeStep',dt);
just takes every 10th point. Often when you do downsampling like that you'd want 'mean', not 'linear', but I think you are concerned with frequency content and that's what resample is all about.
I've made a note to add some more information about this to the resample doc page.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!