Fitting real data curve to a homogeneous diff
显示 更早的评论
Hello! I measured accelerations with matlab Mobile and I encountered the problem that the time step was not homogeneous and therefore the measured frequency. Therefore, I created a homogeneous diff (blue curve) and I would like to find a code in MATLAB that adapts the measured function with the real values (red curve) to that homogeneous time increment.
I attach a picture of the problem.
采纳的回答
8 个评论
Hello! Thanks for the reply.
There is the red curve with the real values, and the blue curve with an homogeneous timestamp (created from the red curve). I want the blue curve to be as similar as possible to the red curve.
Do you know any interpolation function that is very very accurate?
Thanks
Without having the data to work with, I cannot add anything to what I wrote earlier.
The code is below. What it does is to homogenize the time step, and it uses an interp1. I don't have the file, but it consists of a column with the time in seconds and a column with the acceleration measurements.
FICHERO = '2019-02-12-01-24-51-FRacels.csv'; COLTIME = 1; COLACEL = 1; fs = 100;
data = load(FICHERO); t = (data(:, COLTIME) - data(1, COLTIME)) / 1000; v = data(:, 2:end);
% Interpolate data to assure constant time interval at fs dt = 1 / fs; tq = zeros(1,length(data)); for i = 2:length(data) tq(i) = tq(i-1) + dt; end
vq = interp1(t, v(:,COLACEL), tq,'spline','extrap'); %vq = makima(t, v(:,COLACEL), tq); %'linear'); %'nearest'); %'pchip'); %'cubic'); %'makima'); %'spline');
% Reconstruct the data vector data = [tq', vq'];
% Plot original data and interpolated data line figure plot(t, v(:,COLACEL), 'xr-') hold on plot(tq, vq, 'ob-'); legend('Samples', 'Spline Interpolation') xlim([t(1) t(end)]) title('Original and interpolated data')
figure plot(diff(t),'xr') hold on plot(diff(tq),'ob-') legend('Samples', 'Spline Interpolation')
Without having the data to work with, I cannot add anything to what I wrote earlier.
I attach the full code and the file with the acceleration measurements.
But the "Timestamp correction" don't work.
I am not certain what result you want.
This resamples all of them to a common sampling frequency. There are some obvious differences between the original and resampled signals.
The resample function has a number of options, including the interplation method (I chose the default 'linear' method for all of these). You can slso choose a different sampling frequency (I chose 10 Hz here because it is closest to the actual sampling frequency), although that may create data where no data previously existed, so I advise against it.
LD = load('Prueba.mat')
LD = struct with fields:
Acceleration: [53×3 timetable]
Acceleration = LD.Acceleration
Acceleration = 53×3 timetable
Timestamp X Y Z
________________________ _________ ________ ______
03-May-2023 10:44:45.299 0.13878 0.22851 9.7723
03-May-2023 10:44:45.399 0.11067 0.26082 9.7846
03-May-2023 10:44:45.499 0.14267 0.25065 9.9921
03-May-2023 10:44:45.599 0.10857 0.367 9.551
03-May-2023 10:44:45.699 0.085244 0.41575 9.9479
03-May-2023 10:44:45.799 0.034098 0.23659 10.891
03-May-2023 10:44:45.899 0.14656 0.18036 9.5495
03-May-2023 10:44:45.999 0.16839 0.533 9.5333
03-May-2023 10:44:46.099 -0.085244 0.54646 10.048
03-May-2023 10:44:46.199 -0.11396 0.70947 9.1247
03-May-2023 10:44:46.299 0.20489 1.0379 7.2703
03-May-2023 10:44:46.399 0.068495 0.15165 8.4736
03-May-2023 10:44:46.499 -0.27159 -0.26889 13.35
03-May-2023 10:44:46.599 0.24796 -0.33918 12.625
03-May-2023 10:44:46.699 -0.49741 0.40229 9.0996
03-May-2023 10:44:46.799 0.046361 0.84347 6.553
VN = Acceleration.Properties.VariableNames;
[h,m,s] = hms(Acceleration.Timestamp);
s
s = 53×1
45.2990
45.3990
45.4990
45.5990
45.6990
45.7990
45.8990
45.9990
46.0990
46.1990
% X = Acceleration.X
% Y = Acceleration.Y
% Z = Acceleration.Z
format long
ds = diff(s);
ds_stats = [min(ds); max(ds); mean(ds); std(ds); 1/mean(ds)] % 'Original' Time Vector Statistics
ds_stats = 5×1
0.099999999999994
0.100999999999999
0.100019230769231
0.000138675049056
9.998077292828301
Fs = ds_stats(end)
Fs =
9.998077292828301
Fsc = ceil(Fs)
Fsc =
10
Accelerationr = resample(Acceleration, Fsc) % 'Accelerationr' Is The Resampled 'Acceleration' 'timetable'
Accelerationr = 53×3 timetable
Time X Y Z
________________________ _________ _________ _________
03-May-2023 10:44:45.299 0.138784 0.228515 9.772287
03-May-2023 10:44:45.399 0.110668 0.260818 9.784551
03-May-2023 10:44:45.499 0.142672 0.250648 9.992128
03-May-2023 10:44:45.599 0.108574 0.366999 9.550952
03-May-2023 10:44:45.699 0.085244 0.415753 9.947861
03-May-2023 10:44:45.799 0.034098 0.23659 10.891232
03-May-2023 10:44:45.899 0.14656 0.180359 9.549456
03-May-2023 10:44:45.999 0.168395 0.533001 9.533304
03-May-2023 10:44:46.099 -0.085244 0.546461 10.04836
03-May-2023 10:44:46.199 -0.113958 0.709472 9.12473
03-May-2023 10:44:46.299 0.204885 1.037887 7.270292
03-May-2023 10:44:46.399 0.068495 0.151645 8.473583
03-May-2023 10:44:46.499 -0.271585 -0.268893 13.349558
03-May-2023 10:44:46.599 0.247956 -0.339183 12.625429
03-May-2023 10:44:46.699 -0.497408 0.402293 9.099606
03-May-2023 10:44:46.799 0.046361 0.84347 6.553044
[h,m,s] = hms(Accelerationr.Time);
ds = diff(s);
ds_stats = [min(ds); max(ds); mean(ds); std(ds); 1/mean(ds)] % 'Resampled' Time Vector Statistics
ds_stats = 5×1
0.099999999999994
0.100000000000001
0.100000000000000
0.000000000000003
9.999999999999995
figure
tiledlayout(3,1)
for k = 1:3
nexttile
plot(Acceleration.Timestamp, Acceleration{:,k})
title(VN{k})
grid
ylim('padded')
end
sgtitle('Original')

figure
tiledlayout(3,1)
for k = 1:3
nexttile
plot(Accelerationr.Time, Accelerationr{:,k})
grid
title(VN{k})
ylim('padded')
end
sgtitle('Resampled')

figure
tiledlayout(3,1)
for k = 1:3
nexttile
plot(Acceleration.Timestamp, Acceleration{:,k}, 'DisplayName','Original')
hold on
plot(Accelerationr.Time, Accelerationr{:,k}, 'DisplayName','Resampled')
hold off
grid
title(VN{k})
if k == 1
legend('Location','best')
end
ylim('padded')
end
sgtitle('Original Co-Plotted With Resampled')

figure
k = 2;
plot(Acceleration.Timestamp, Acceleration{:,k}, 'DisplayName','Original')
hold on
plot(Accelerationr.Time, Accelerationr{:,k}, 'DisplayName','Resampled')
hold off
grid
title(VN{k})
legend('Location','best')

I am not certain what part of what signal is in the originally provided plot image, or I would post it in detail here.
.
Hello!
Thanks for your help. The problem is that you use the mean frecuency, so there is still a non homogeneus sample frecuency.
I have finally solved the problem.
Pd.: Do I have to accept your answer? I thank you very much for the help.
data=[t,z];
COLTIME = 1;
COLACEL = 1;
tt = (data(:, COLTIME) - data(1, COLTIME));
v = data(:, 2:end);
% Interpolate data to assure constant time interval at fs:
dt = 1 / fs;
ntime = length(data);
tq = zeros(1,ntime);
for i = 2:ntime
tq(i) = tq(i-1) + dt;
end
vq = interp1(tt, v(:,COLACEL), tq,'spline','extrap');
% Reconstruct the data vector:
data = [tq', vq'];
I would appreciate it if you would accept my answer.
I actually use the most common frequency, that I derive from the differences in the sampling intervals. I use mean, I also could have used mode.
I notice the the code you posted uses ‘fs’ to define ‘dt’ without first defining ‘fs’. I have no idea how you define ‘fs’.
I also use resample instead of interp1 because resample uses an anti-aliasing filter. This is important for signal processing applications, and signal processing is apprarently what you want to do.
The resample function also has the 'spline' option as an interpolation method. I rarely use it (preferring 'pchip') because it can give some wildly varying results.
.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Multirate Signal Processing 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
