Interpolated data is drifting downward
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (2)
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');
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'))
Fs = LD.Fs;
test = LD.test
VN = test.Properties.VariableNames;
t = test.t;
s = test{:,[2 3]};
Tsm = mode(diff(t)) % Most Frequent Value
Fsm = 1/Tsm % Most Frequent Value
[sr,tr] = resample(s,t,Fsm); % Resample, Return New Time Vector & Signal Matricx
length_difference = numel(tr) - numel(t)
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.).
.
0 commentaires
Voir également
Catégories
En savoir plus sur Multirate Signal Processing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!