Interpolated data is drifting downward

1 vue (au cours des 30 derniers jours)
Lukas
Lukas le 14 Mar 2023
idx = find(T.Fs~=1024)
t=test.t
for i = 2:length(idx)
Gap = t(idx(i))-t(idx(i)-1)
check(i,1) = Gap*1024
end
old_time = t
new_time = old_time(1:idx(2)-1);
for i = 2:length(idx)
if idx(i) < length(old_time) && i ~= length(idx)
interp_time = interp1(old_time(idx(i)-1:idx(i)),(1:check(i)), 'linear','extrap');
new_time = [new_time; interp_time(:); old_time(idx(i)+1:idx(i+1)-1)];
else
interp_time = interp1(old_time(idx(i)-1:idx(i)), (1:check(i)), 'linear','extrap');
new_time = [new_time; interp_time(:); old_time(idx(i)+1:end)];
end
end
I'm trying to interpolate certain sections of my data because the accelerometer occasionally has drops in sample rate. I use the first loop to check the number of samples needed to upsample to 1024hz. The second loop interpolates the data and concatenates the interpolated data in place of the dropped sample rate.
My problem is when I plot new_time and old_time, there is a downward drift in my new time. Is there a way I can fix this?
NOTE: the table T is the calculated sample rate at each instance in time

Réponses (2)

Mathieu NOE
Mathieu NOE le 15 Mar 2023
hello
I don't really understand your problem and why you make the code so complicated
I assumed the issue is simply you have missing samples or variable sampling rate
I also assume (seeing t is a very big number) that the original t values are in units like micro (nano ?) seconds
this example show how to resample your data to a fixed , known, different rate.
You could also use fillmissing
t=test.t;
data = test.high;
t = (t - t(1))*1e-6; % make t start at t = 0, and convert from microseconds (?, to be confired) to seconds
% resample at Fs = 1024 Hz
Fs = 1024;
dt = 1/Fs;
newt = min(t):dt:max(t);
newdata = interp1(t,data,newt);
plot(t,data,newt,newdata);
xlabel('Time (s)');
legend('original data','resampled data');
  1 commentaire
Mathieu NOE
Mathieu NOE le 6 Avr 2023
hello
problem solved ?

Connectez-vous pour commenter.


Star Strider
Star Strider le 6 Avr 2023
The Signal Processing Toolbox resample function is likely the best option here, at least in part because it uses an anti-aliasing filter, not an option with interp1
LD = load(websave('matlab','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1324365/matlab.mat'))
LD = struct with fields:
Fs: 1024 Gap: 4.8828 T: [329354×1 table] check: [13×1 double] h: [329355×1 double] i: 13 idx: [13×1 double] s: [329354×1 single] t: [329355×1 double] test: [329355×4 table]
Fs = LD.Fs;
test = LD.test
test = 329355×4 table
t high Sync_Bridge_Amp_Low_CAL VarName4 __________ _______ _______________________ ________ 1.6783e+12 0.79775 0.68594 "" 1.6783e+12 0.7685 0.65802 "" 1.6783e+12 0.80307 0.72582 "" 1.6783e+12 0.81371 0.72183 "" 1.6783e+12 0.82434 0.71386 "" 1.6783e+12 0.82966 0.72183 "" 1.6783e+12 0.83099 0.74177 "" 1.6783e+12 0.83764 0.75773 "" 1.6783e+12 0.84827 0.75773 "" 1.6783e+12 0.86024 0.74576 "" 1.6783e+12 0.86556 0.76171 "" 1.6783e+12 0.86556 0.78165 "" 1.6783e+12 0.87221 0.79362 "" 1.6783e+12 0.8855 0.79362 "" 1.6783e+12 0.89348 0.78165 "" 1.6783e+12 0.8988 0.78963 ""
VN = test.Properties.VariableNames;
t = test.t;
s = test{:,[2 3]};
Tsm = mode(diff(t)) % Most Frequent Value
Tsm = 0.9766
Fsm = 1/Tsm % Most Frequent Value
Fsm = 1.0240
[sr,tr] = resample(s,t,Fsm); % Resample, Return New Time Vector & Signal Matricx
length_difference = numel(tr) - numel(t)
length_difference = 167
figure
plot(tr,sr) % Plot Interpolated Time & Signal Matrix
grid
xlabel('Time (Resampled)')
ylabel('Signals (Resampled)')
legend(VN{2}, strrep(VN{3},'_','\_'), 'Location','best')
The resampled vectors are uniformly xampled at the sampling freqeuency ‘Fsm’, that being 1.024 Hz. You can resample it at any frequency that you like, noting that sampling it at a significantly higher frequency creates new data where no data currently exist. Sampling it as I did here minimises that prpblem, since the sampling frequency is essentially the most frequent sampling frequency in the signal. The resampling creates by linear interpolation (other method options are possible) 167 new data here, about 0.05% of the new total vector lengths.
Now that it has a consistent sampling frequency, it can be used in all signal processing approaches (filtering, Fourier transform, etc.).
.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by