Integrating above and below x axis separately

조회 수: 1 (최근 30일)
LRW
LRW 2020년 12월 23일
댓글: Image Analyst 2020년 12월 23일
I have 90 trials of walking data that vary in duration, but the longest lasting 93 timestamps (so, I have a 93X90 matrix named Powers). Below is a plot of one trial, where the x axis is time and the y axis is Joint Power:
I am wanting to integrate the curve below and above the x axis separately so that I have the total positive and negative work. I have tried using if end statements and the trapz function but that didnt seem to work and I'm not sure why.
if Powers > 0
PosWork = trapz(Powers,1)
end
if Powers < 0
NegWork = trapz(Powers, 1)
end
How would I integrate a curve below and above the x axis separately for every column of my matrix? Any help is appreciated. Thank you in advance.

채택된 답변

Star Strider
Star Strider 2020년 12월 23일
With no if blocks:
t = linspace(0, 10); % Independent Variable
Powers = sin(2*pi*t) + randn(size(t))*0.1; % Signal (With Some Noise)
Poswork = trapz(t, Powers .* (Powers > 0)); % With Independent Variable (More Accurate)
Negwork = trapz(t, Powers .* (Powers < 0)); % With Independent Variable (More Accurate)
Poswork = trapz(Powers(:) .* (Powers(:) > 0), 1); % With Point Spacing, Forcing Column Vector
Negwork = trapz(Powers(:) .* (Powers(:) < 0), 1); % With Point Spacing, Forcing Column Vector
.
  댓글 수: 2
LRW
LRW 2020년 12월 23일
This worked. Thank you!
Star Strider
Star Strider 2020년 12월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 12월 23일
Try
if Powers > 0
p2 = Powers
p2(p2 <= 0) = 0; % Zero out negative going values.
PosWork = trapz(p2, 1)
end
if Powers < 0
p2 = Powers
p2(p2 >= 0) = 0; % Zero out positive going values.
NegWork = trapz(p2, 1)
end
  댓글 수: 5
Image Analyst
Image Analyst 2020년 12월 23일
Looks like you no longer need help since you accepted Star's answer.
Image Analyst
Image Analyst 2020년 12월 23일
OK, I did it because it doesn't look like Star's solution reads in your data and does it over all your columns like you wanted. Try this:
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
Powers = csvread('Powers.csv')
x = Powers(:, 1);
subplot(1, 2, 1);
[rows, columns] = size(Powers);
for col = 2 : columns
thisY = Powers(:, col);
thisX = x;
% Remove any nans
nanInidexes = isnan(thisY);
thisY(nanInidexes) = [];
thisX(nanInidexes) = [];
p2 = thisY; % Initialize
% Plot it.
plot(thisX, thisY, '-');
grid on;
hold on;
p2(thisY <= 0) = 0; % Zero out negative going values.
PosWork(col-1) = trapz(p2, 1)
p2 = thisY; % Re-initialize.
p2(thisY >= 0) = 0; % Zero out positive going values.
NegWork(col-1) = trapz(p2, 1)
end
title('Individual Data', 'fontSize', fontSize);
subplot(1, 2, 2);
plot(PosWork, 'b-', 'LineWidth', 2);
hold on;
plot(NegWork, 'b-', 'LineWidth', 2);
grid on;
xlabel('x', 'fontSize', fontSize);
ylabel('Work', 'fontSize', fontSize);
legend('Positive Work', 'Negative Work');
title('Work', 'fontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m ...\n', mfilename);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by