From: Nicholas Phillips on 17 Apr 2010 14:10 "isha ram" <roshni_balan(a)hotmail.com> wrote in message <hqa1k4$7b3$1(a)fred.mathworks.com>... > can anybody tell how to calculate the moving average at a lag of 10 of about 65000 data > thanks in advance > ex: > t = [0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10] > Q=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] > when t = 5; Q = average(1:10) > t = 10; Q=average(11:20) Isha, This is tough because I dont fully understand what you are asking to do. How many averages are you wanting to include in your moving average? From what I gather it looks like 10 averages but is it allways 10 averages? Are you wanting to average Q or t? If I am guessing correctly Q is your index array, meaning that t=5 corresponds to Q = 10, therfor being lag 10, and at lag 10 you will want the average the last 10 samples ot t, being: mov_ave_at_lag_10 = mean(t(1:10)) ; This shouldn't be too difficult, the following piece of code will do this, as mentioned above from other responses there are more effecient ways to do this, via filter, conv, etc but this piece of code will hopefully be more explicit to help yo understand what is happening. It will calculate the 10 point moving avereage if 10 points are availible, if not (the begining of the t sequence) it will use as many points as it can. t = rand(1,65000); Q = 1:length(t); tic M = 10; %number of points in average k = 0:M-1; N = length(Q); for n = 1:N if n > M-1 mov_ave(n) = sum(t(n-k))/M; else mov_ave(n) = sum(t(n-k(1:n)))/n; end end t1=toc; The problem is for a datastream of 65000 samples this will take a few seconds to run. To speed things up we can use the ideas of filter or conv, which is basically the same thing as to implement a filter in the time domain we convolve it. a = 1; b = ones(1,M)/M; %M is sill the number of points in our moving average tic; mov_ave2 = conv(t,b); t2 = toc;tic; mov_ave3 = filter(b,a,t);t3=toc; figure; subplot(311); plot(mov_ave) title('explicit moving average') subplot(312); plot(mov_ave2) title('moving average by convolution') subplot(313); plot(mov_ave3) title('moving average using filter') From the plots you should see that other than the endpoints the three methods are identical. Hope this was helpful, Nick
From: isha ram on 19 Apr 2010 03:00 "Nicholas Phillips" <np7(a)cec.wustl.edu> wrote in message <hqctid$jli$1(a)fred.mathworks.com>... > "isha ram" <roshni_balan(a)hotmail.com> wrote in message <hqa1k4$7b3$1(a)fred.mathworks.com>... > > can anybody tell how to calculate the moving average at a lag of 10 of about 65000 data > > thanks in advance > > ex: > > t = [0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10] > > Q=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] > > when t = 5; Q = average(1:10) > > t = 10; Q=average(11:20) > > Isha, > > This is tough because I dont fully understand what you are asking to do. How many averages are you wanting to include in your moving average? From what I gather it looks like 10 averages but is it allways 10 averages? Are you wanting to average Q or t? If I am guessing correctly Q is your index array, meaning that t=5 corresponds to Q = 10, therfor being lag 10, and at lag 10 you will want the average the last 10 samples ot t, being: > > mov_ave_at_lag_10 = mean(t(1:10)) ; > > This shouldn't be too difficult, the following piece of code will do this, as mentioned above from other responses there are more effecient ways to do this, via filter, conv, etc but this piece of code will hopefully be more explicit to help yo understand what is happening. It will calculate the 10 point moving avereage if 10 points are availible, if not (the begining of the t sequence) it will use as many points as it can. > > t = rand(1,65000); > Q = 1:length(t); > tic > M = 10; %number of points in average > k = 0:M-1; > N = length(Q); > for n = 1:N > if n > M-1 > mov_ave(n) = sum(t(n-k))/M; > else > mov_ave(n) = sum(t(n-k(1:n)))/n; > end > end > t1=toc; > > The problem is for a datastream of 65000 samples this will take a few seconds to run. To speed things up we can use the ideas of filter or conv, which is basically the same thing as to implement a filter in the time domain we convolve it. > > a = 1; > b = ones(1,M)/M; %M is sill the number of points in our moving average > tic; > mov_ave2 = conv(t,b); t2 = toc;tic; > mov_ave3 = filter(b,a,t);t3=toc; > > figure; > subplot(311); plot(mov_ave) > title('explicit moving average') > subplot(312); plot(mov_ave2) > title('moving average by convolution') > subplot(313); plot(mov_ave3) > title('moving average using filter') > > From the plots you should see that other than the endpoints the three methods are identical. > > Hope this was helpful, > > Nick thanks Nick, i think this will work out
First
|
Prev
|
Pages: 1 2 Prev: using vectors as indices Next: Real-Time Windows Target and RS232 communication? |