High pass filter on cumtrapz
Show older comments
Hi! I am new to signal processing and in Matlab so my question may look dumb, but I was having trouble on the getting the graph that i want on my velocity and displacement. I want to use a highpass filter and not detrend code so i wont have to edit the order everytime i input another data. I attached the example file that i was working on too. I would really appreciate the help.
clear all;
close all;
clc;
%raw data from XYZ direction
NormChest = readtable('Chest-Normal.csv'); %%%get the raw data
C = table2array(NormChest(:,1));
x = table2array(NormChest(:,2));
y = table2array(NormChest(:,3));
z = table2array(NormChest(:,4));
time = C(:,1);
mag = table2array(NormChest(:,5));
%%design for low pass filter
fs = 1000; %sampling frequency %%%trial first
fc = 20; %cut-off frequency %%%trial first
order = 2; %6th order filter %%%removes any low frequency
%%Filter acceleration signals
[b1, a1] = butter (order, fc/(fs/2));
acceleration = filtfilt (b1, a1, mag);
figure (2)
plot (time, mag); hold on;
plot (time, acceleration, 'r'); hold on; axis tight;
xlabel('Time'); ylabel('Acceleration');
legend ('Raw', 'Filtered', 'Orientation','vertical', 'Location','northeast')
%first integration (acceleration-velocity)
fcv = 6; %cut-off frequency
orderv = 6; %6th order filter
[b2, a2] = butter (orderv, fcv, "high");
fvtool = ([b2, a2]);
velocity = cumtrapz(time, acceleration);
plot (time, velocity);axis tight;
xlabel('Time'); ylabel('Velocity');
%%second integration (velocity-displacement)
displacement = cumtrapz(time, velocity);
%displacement = detrend (displacement, 6); %%%detrend slope graph
figure (4)
plot (time, displacement); axis tight;
xlabel('Time'); ylabel('Displacement');
time = time - time(1);
Accepted Answer
More Answers (1)
Hassaan
on 8 Jan 2024
clear all;
close all;
clc;
% Load data from the CSV file
NormChest = readtable('Chest-Normal.csv');
C = table2array(NormChest(:,1));
x = table2array(NormChest(:,2));
y = table2array(NormChest(:,3));
z = table2array(NormChest(:,4));
time = C(:,1);
mag = table2array(NormChest(:,5));
% Design a high-pass filter to remove low-frequency drift
fs = 1000; % Sampling frequency
fchp = 0.5; % Cut-off frequency for high-pass filter, choose based on your data
order = 2; % Define the order of the filter, e.g., 1, 2, etc.
[b1, a1] = butter(order, fchp/(fs/2), 'high'); % 'high' specifies a high-pass filter
% Filter the magnitude of acceleration
filteredMag = filtfilt(b1, a1, mag);
% Plot raw vs filtered acceleration
figure;
plot(time, mag); hold on;
plot(time, filteredMag, 'r');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
legend('Raw', 'Filtered');
title('Acceleration Data');
hold off;
% First integration (from acceleration to velocity)
velocity = cumtrapz(time, filteredMag);
% You might want to apply another high-pass filter to the velocity if there's still a drift
[b2, a2] = butter(order, fchp/(fs/2), 'high');
filteredVelocity = filtfilt(b2, a2, velocity);
% Plot the velocity
figure;
plot(time, filteredVelocity);
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('Velocity Data');
axis tight;
% Second integration (from velocity to displacement)
displacement = cumtrapz(time, filteredVelocity);
% Again, you might want to apply a high-pass filter to the displacement if needed
[b3, a3] = butter(order, fchp/(fs/2), 'high');
filteredDisplacement = filtfilt(b3, a3, displacement);
% Plot the displacement
figure;
plot(time, filteredDisplacement);
xlabel('Time (s)');
ylabel('Displacement (m)');
title('Displacement Data');
axis tight;
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Categories
Find more on Chebyshev in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

