How can I get the mean data from under the graphs?
3 views (last 30 days)
Show older comments
Carmen Brouwers
on 30 Jun 2022
Commented: Star Strider
on 1 Jul 2022
Hi,
I am struggeling with getting the mean values from my data.
When plotted my data looks like this (this is only a small sample of the whole thing). I want to get the mean values of the signal from everything above 0. But not the noise you see around the 0 yline, the data is already filtered at the frequency it needs to be. I have tried it with manually looking up the range of the data which I needed and then:
data = [all_data(39:111) all_data(150:171)]
meandata = mean(data(data>0))
But this would take me years to manually look up every data point I need. I thought maybe a loop could help by telling MATLAB if the value is >0 for a period of 10 or more sample then sum/mean but I can't figure out how to do this?
Hope anyone can help me, thanks in advance!
0 Comments
Accepted Answer
Star Strider
on 30 Jun 2022
To isolate the regions around the peaks, do something like this —
x = linspace(0, 150, 500); % Create Data (Row)
y = @(v) sinc((x+v)-mean((x+v))/5); % Create Data (Rows)
v = (0:10:1500).';
x = x(:); % Convert To Column
y = sum(y(v)).'; % Convert To Column
y = y + rand(size(y))*0.2; % Add Noise
figure
plot(x, y)
grid
xlabel('X')
ylabel('Y')
title('Original Data')
[pks,plocs] = findpeaks(y, 'MinPeakProminence',1); % Desired Peak & Index
for k = 1:numel(pks)
[vys,vlocs] = findpeaks(-y, 'MinPeakProminence',0.1); % Valleys & Indices
[vlc,ixv] = mink(abs(vlocs-plocs(k)),2); % Choose Two Closest Valleys To Desired Peak
vlcs(:,k) = plocs(k) + [-1; 1].*vlc; % Calculate Indices
end
% vlcs
figure
plot(x, y)
hold on
for k = 1:size(vlcs,2)
idxr = vlcs(1,k) : vlcs(2,k); % Initial Index Range
xv = x(idxr);
yv = y(idxr);
Apos(k,:) = trapz(x(yv>=0),yv(yv>=0)); % Calculate Positive Peak Areas
Mpos(k,:) = mean(yv(yv>=0)); % Calculate Mean Value
plot(x(idxr), y(idxr), '-r') % Plot Desired Peak Region
end
hold off
grid
xlabel('X')
ylabel('Y')
title('Original Data With Selected Regions Higlighted')
PeakAreas = table(x(plocs),Apos,Mpos, 'VariableNames',{'Peak X Location','Peak Area','Peak Mean'})
The regions in red in the second plot are the segments used for the ‘Apos’ area and ‘Mpos’ mean calculations.
.
2 Comments
More Answers (1)
Jonas
on 30 Jun 2022
what about a general threshold like 10 here?
mean(all_data(all_data>10))
2 Comments
See Also
Categories
Find more on Whos 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!