How to calculate differences of two floating means in a vector of ~10^6 entries?

1 view (last 30 days)
Hello, everyone!
Unfortunately I am not familiar enough with Matlab, so I need your help to learn and succeed!
I am trying to analyse a measurement signal that comes in an array "v" of ~10^6 rows and 1 column.
Target ist to count the numbers of amplitudes that have a certain hight.
Since the signal is overlayed with noise,
I need to smoothen it (I know already about filtering, but this did not work out properly).
I got a rough plan how to do it - these are the steps I want to perform
  1. calculate the mean from the first to the 200th entry of v
  2. calculate the mean from the 201th to the 400th entry of v
  3. calculate the difference between step 2 and 1
  4. store the result in a new array e.g. "w"
  5. repeat this from the first to the last entry of the vector v
  6. with having the complete vector "w" containing all differences count the amount of elements that are larger than 3 and smaller than -3
For step 1 to 5 I assume to use a for loop, but don't know how to do it
I post the code, that resembles my ideas
v=Im_GPA %load measurement signal and make it a new variable
k=200 % define the step width for calculation of mean
w=zeros([length(v) 1]) % initialize a vector with 0 to put down the results of step 1 to 5
for i = (1:length(v))
a = sum(vi : vi*k)/k
b = sum(vi+k : v(i+1)*k)/k
c = b-a % put it into wi
end
w_low=sum(w<3);
w_high=sum(w>3);
w_total = w_low+w_high;
disp(w_total)

Accepted Answer

John D'Errico
John D'Errico on 5 Dec 2018
Edited: John D'Errico on 5 Dec 2018
Start using MATLAB, in the way it was designed to be used. It seems here, you want to compute a floating mean, a term I've not heard before, but it means whatever you want it to indicate. Here, it seems to indicate a mean over each block of 200 elements.
So just reshape your vector. Then use the function mean. For example, what happens if we do this:
vr = reshape(v,200,[]);
It reshapes the vector into a new array, with 200 rows, and numel(v)/200 columns. Of course, if you don't have a multiple of 200 for the length, then the last block will be a problem because the reshape would fail. I would then suggest padding the vector to have NaNs at the end in that case. Then you could use the function nanmean to compute the means.
The point is, to start thinking in MATLAB terms. That means thinking more in terms of arrays, vectors. Not single elements. So, now what happens if you do this (I'll break it down into separate lines to make it easier to follow.)
vmean = mean(vr,1);
So all of your means are done. No loop needed. If you wanted to compute the difference between successive means, then use diff. Again, one line.
vmeandiff = diff(vmean);
Now you can do whatever tests you like on those differences. Again, think in terms of a vector. It looks like you somehow knew to do that at the end, since you know what the function sum does. So to be honest, I think your problem is you need to learn more about the basic tools in MATLAB. you knew about sum, but clearly not mean or diff. (I.e., you knew that to compute a mean, you used sum, then divided by the number of elements. So you have no idea that mean exists as a function.) That suggests you either need to visit the getting started tutorials, or just start reading through the lists of functions in MATLAB. Read the help for each one that seems interesting.
For example, if I do this in MATLAB:
which sum -all
I would find that sum lives in the datafun directory. So then I might try
help datafun
That gives a nice compact list of ALL the tools in MATLAB in that directory, one line for each, with a short description of each. Read through there. Are there some tools that you would find useful? If so, then click on them. Get the help for the ones you think are interesting and possibly useful. This kind of exploratory earch through the help is a simple way to quickly learn MATLAB.
  2 Comments
Lucas Santin
Lucas Santin on 5 Dec 2018
Dear John!
First of all - Thank you very much. You directly and precisely answered my question.
Second - you are definitely right, I need to understand the syntax or language of MATLAB.
This I obviously do not do right now. I will take my time to start with the getting started tutorials and continously improve.
Once again - Thank you! I am encouraged and motivated to start the journey in MATLAB!
John D'Errico
John D'Errico on 5 Dec 2018
As I said, you are started in the right direction, since you usedsum. That means you understand the dieaof using functions that operate on vectors. Now it is just getting used to using more tools, mean, diff, etc. That part goes fast once you understand the conceptts.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!