Hi @Katie,
To address your first query, “I have some motion capture data that I need to filter before differentiating to get velocity and acceleration. I have been told to filter at a range of frequencies and then calculate the RMSD between the original and filtered data, I want to used a forth order low pass butterworth filter.“
Please see updated code snippet with attached plots, because if you study the RMSD plot in attached modified code snippet of yours along with FFT analysis, you will be able to figure out making an informed decision on selecting the most suitable filtering frequency for your motion capture data processing. Here is your attached modified code snippet,
% Define and initialize sample data and parameters
data = randn(1, 50); % Sample data
fs = 1000; % Sample sampling frequency
function plot_rmsd_vs_cutoff_with_fft(data, fs, f_min, f_max, num_points)
cutoff_freqs = linspace(f_min, f_max, num_points);
rmsd_values = zeros(size(cutoff_freqs));
% Initialize a counter to keep track of the number of plots
plot_counter = 0;
for i = 1:length(cutoff_freqs)
[b, a] = butter(4, cutoff_freqs(i)/(fs/2), 'low');
filtered_data = filtfilt(b, a, data);
% Calculate RMSD
rmsd_values(i) = rms(data - filtered_data);
% FFT Analysis
original_fft = fft(data);
filtered_fft = fft(filtered_data);
% Plot FFT results for comparison
if plot_counter < 2
figure;
subplot(2,1,1);
plot(abs(original_fft));
title('Original Data FFT');
subplot(2,1,2);
plot(abs(filtered_fft));
title('Filtered Data FFT');
plot_counter = plot_counter + 1;
end
end
% Plot RMSD against cut-off frequency
figure;
plot(cutoff_freqs, rmsd_values);
xlabel('Cut-off Frequency (Hz)');
ylabel('RMSD');
title('RMSD vs Cut-off Frequency');
end
% Call the function with the defined data
plot_rmsd_vs_cutoff_with_fft(data, fs, 1, 50, 50);
Please see attached plot.
Addressing your next query regarding, “I have managed to do so but do not understand how I then interpret the plot to decide on my optimal filtering frequency”,
The frequency corresponding to minimum RMSD value is considered the optimal filtering frequency. This frequency represents the point where the filter performance is most effective in reducing noise or unwanted components in the signal while preserving the desired information.
Now, coming back to your query about, “I have seen some previous posts mention 'fft' but do not understand what this does and if I should be using it.”
In nutshell, it is used to analyze frequency content in signals. In this context, it can be used to examine the frequency of your data before and after filtering which can be you understand how the filter affects different frequency components and guide you to your choice of the optimal frequency.
Hope, this answers all your questions. Please let me know if you have any further questions.