Azzera filtri
Azzera filtri

peak average of consecutive values in a matrix array

2 visualizzazioni (ultimi 30 giorni)
Hello, I would like to calculate the average of the peaks (positive and negative values) of consecutive values from a predifined number of elements from a Nx1 matrix, where N is the number of rows. For example, let's say I have a matrix with the following form (24x1):
A[1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24]
I want to obtain the peak and the average for every 5 elements,
B=[mean(peak(1,2,3,4,5)); mean(peak((2,3,4,5,6)); mean(peak((3,4,5,6,7));...........mean(peak((20,21,22,23,24))]
I am chosing every 5 elements to simplify the example. But, this number number of elements can be higher than 100 and the points vary dramatically from positive to negative as they come from acceleration earthquake signals from N higher than 245000.
I would appreciate the help.
  6 Commenti
Dyuman Joshi
Dyuman Joshi il 19 Lug 2023
Suppose this is the data in hand -
x = randi([-20 20],1,15)
x = 1×15
-5 -16 14 -18 19 -3 18 1 -10 -6 -12 16 5 8 0
Assume the moving window is 4, what should be the output in thise case? and what is the logic behind it?

Accedi per commentare.

Risposta accettata

Mathieu NOE
Mathieu NOE il 19 Lug 2023
hello
maybe this ?
I am using peakseek , this fex submission is available here
, but you can use the standard (slower) findpeaks function offered by TMW
the code provided below will buffer your data with length = 51 samples , then split the signal for each cahnnel into a positive and negative signal and find the positive and negative peaks , then take the mean of them (separately not mixing pos and neg peaks !)
example for X channel :
data = readmatrix('Signal.txt'); % acceleration earthquake signal in cm/sec2 ,X, Y and Z
[samples, channels] = size(data);
dt = 1; % sample rate (s)
t = dt*(0:samples-1); % time vector
%% home made solution (you choose the amount of overlap)
buffer_size = 51; % how many samples
overlap = 0; % overlap expressed in samples
%%%% main loop %%%%
[new_time,data_out_pos,data_out_neg] = my_peakmean(t,data,buffer_size,overlap);
figure(1),
plot(t,data(:,1),new_time,data_out_pos(:,1),'*-g',new_time,data_out_neg(:,1),'*-r');
title('X');
legend('raw data','pos peaks mean','neg peaks mean');
xlabel('Time(s)');
ylabel('cm/sec2');
figure(2),
plot(t,data(:,2),new_time,data_out_pos(:,2),'*-g',new_time,data_out_neg(:,2),'*-r');
title('Y');
legend('raw data','pos peaks mean','neg peaks mean');
xlabel('Time(s)');
ylabel('cm/sec2');
figure(3),
plot(t,data(:,3),new_time,data_out_pos(:,3),'*-g',new_time,data_out_neg(:,3),'*-r');
title('Z');
legend('raw data','pos peaks mean','neg peaks mean');
xlabel('Time(s)');
ylabel('cm/sec2');
%%%%%%%%%% my functions %%%%%%%%%%%%%%
function [new_time,data_out_pos,data_out_neg] = my_peakmean(t,data_in,buffer_size,overlap)
% NB : buffer size and overlap are integer numbers (samples)
% data (in , out) are 1D arrays (vectors)
shift = buffer_size-overlap; % nb of samples between 2 contiguous buffers
[samples,channels] = size(data_in);
nb_of_loops = fix((samples-buffer_size)/shift +1);
for k=1:nb_of_loops
start_index = 1+(k-1)*shift;
stop_index = min(start_index+ buffer_size-1,samples);
x_index(k) = round((start_index+stop_index)/2);
% find peaks
for n = 1:channels
tmp = data_in(start_index:stop_index,n);
tmp_pos = tmp;
tmp_pos(tmp_pos<0) = 0;
tmp_neg = tmp;
tmp_neg(tmp_neg>0) = 0;
[locsp, pksp]= peakseek(tmp_pos,1,max(tmp_pos)/10); % pos peaks above 10% of max pos amplitude
[locsn, pksn]= peakseek(-tmp_neg,1,max(-tmp_neg)/10); % neg peaks above 10% of max neg amplitude
data_out_pos(k,n) = mean(pksp); % mean of pos peaks
data_out_neg(k,n) = mean(-pksn); % mean of neg peaks
end
end
new_time = t(x_index); % time values are computed at the center of the buffer
end
  5 Commenti
Jorge Luis Paredes Estacio
I really appreaciate your help. it is sorted now. Thank you very much.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by