# Change of frequency content after using the retime function

조회 수: 6 (최근 30일)
Julius Bartasevicius 2023년 11월 16일
편집: Peter Perkins 2023년 11월 17일
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])

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

### 답변 (2개)

Sulaymon Eshkabilov 2023년 11월 17일
Here is how to solve this issue:
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])
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Julius Bartasevicius 2023년 11월 17일
This solution changes the frequency content as well.

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

Peter Perkins 2023년 11월 17일
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);
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Peter Perkins 2023년 11월 17일
편집: Peter Perkins 2023년 11월 17일
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.

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

### 카테고리

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

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by