Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downsampling data in Matlab by averaging [duplicate]

I have 2 data sets in Matlab that I need to plot against one another - one on the xaxis and one on the yaxis. The data for each set was collected using a different method so the sampling rate is significantly different and until I don't the same number of data points in both sets I cannot plot one against the other.

Its quite simple to downsample data in Matlab using the downsample function Matlab.

A = [-3 -1.5 0 1.5 3 4.5 6 7.5 9...] (goes on until 21) 
B = [-3.9 -3.8 -3.7 -3.6 -3.5 -3.5 -3.3 -3.2 -3.1 -3.0 -2.9 -2.8...] (goes on until 22) 

The sampling rate of A is 1.5s and the sampling rate of B is 0.1s. I have been able to successfully use downsample as downsample(B,15,10) to get it to start at the same time "-3s" (which means something in my data so I need to get it to start at that point) and be at the sample sampling rate of 1.5s.

Now, however, I was wondering whether there was a method which allowed me to take the average of the 15 points, instead of picking one point every 15 points? downsample, the way I have used it just picks every 15th point. I, however, would like for it to average the 15 points for me instead. Is there a way of doing this?

I wrote a for loop for a simple/smaller vector to see if I could do it. For A = [1 2 3 4] I would want to condense the data so that A only has 2 entries, such that it averages A(1) and A(2) and then A(3) and A(4).

A = [1 2 3 4] 
for i = 1:3
  P(i) = mean(A(i:i+1))
end 

This, however, does not work like I want it to because I don't want it to average A(2) and A(3). I want it to take the first 2 entries, average them, then the next 2 entries, then average them. so on.

Can anybody help?

Thanks

like image 540
Maheen Siddiqui Avatar asked Sep 29 '22 01:09

Maheen Siddiqui


1 Answers

Reshape your data A into an n-row matrix, where n is the averaging size, and apply mean to compute the average of each column:

A = [1 2 3 4]; %// data
n = 2; %// averaging size
P = mean(reshape(A,n,[]));
like image 175
Luis Mendo Avatar answered Oct 03 '22 07:10

Luis Mendo