40 lines
1.3 KiB
Matlab
40 lines
1.3 KiB
Matlab
function sample_data(t, x, Ts, A0, omega, filename)
|
|
% SAMPLE_DATA - Resamples pendulum simulation data and exports to CSV
|
|
%
|
|
% Usage:
|
|
% sample_pendulum_data(t, x, Ts, A0, omega, filename)
|
|
%
|
|
% Inputs:
|
|
% t - time vector from ODE solver (e.g., from ode45)
|
|
% x - state matrix [q(t), dq(t)]
|
|
% Ts - sampling period (in seconds)
|
|
% A0 - input amplitude for u(t) = A0 * sin(omega * t)
|
|
% omega - input frequency (in rad/s)
|
|
% filename - output CSV filename
|
|
|
|
% Time vector for resampling
|
|
t_sampled = t(1):Ts:t(end);
|
|
|
|
% Resample q and dq using interpolation
|
|
q_sampled = interp1(t, x(:,1), t_sampled);
|
|
dq_sampled = interp1(t, x(:,2), t_sampled);
|
|
|
|
% Estimate second derivative using central finite differences
|
|
ddq_sampled = zeros(size(q_sampled));
|
|
N = length(q_sampled);
|
|
|
|
for k = 2:N-1
|
|
ddq_sampled(k) = (q_sampled(k+1) - 2*q_sampled(k) + q_sampled(k-1)) / Ts^2;
|
|
end
|
|
|
|
% Evaluate input u(t)
|
|
u_sampled = A0 * sin(omega * t_sampled);
|
|
|
|
% Create table and export to CSV
|
|
T = table(t_sampled', q_sampled', dq_sampled', ddq_sampled', u_sampled', ...
|
|
'VariableNames', {'t', 'q', 'dq', 'ddq', 'u'});
|
|
|
|
writetable(T, filename);
|
|
fprintf('Sampling data saved to file: %s\n', filename);
|
|
end
|