From: Sean Douglas on 5 Jun 2010 12:39 sorry i made a mistake with my last question, here it is again. Hey guys I have a moving average code that I asked some advice on before, but i'm still not getting it to work any faster. It is still taking about 10 minutes to run even after adding in a line to preallocate my array. Here is the code I am using on a small example. a= 20041126 7.11 20041127 2.33 20041128 5.88 20041129 4.67 20041130 4.66 20041201 3.01 20041203 2.58 20041204 1.35 MA=3 n=length(a) k=n - MA m=1 Mavg=zeros(1,n+1-MA) %attempt to preallocate the array for q=1:n+1 - MA j=m:MA+m - 1 Mavg(q)=sum(a(j,2))/MA m=m+1 end the actuall data i use is 3000 columns of data and figuring a Moving Average of 150. This data of 8 columns and a Moveing average of 3 is just for an example to ask you guys some advice. I know from before people suggested using conv() and differencing my data and allocating an array. conv() im not quite understanding and i don't know what differencing the data is about, so I am attempting to preallocate an array with this line: Mavg=zeros(1,n+1-MA,1) , but still not working. Please some help to get me started with makeing this moving average calculate faster. thank you, sean
From: us on 5 Jun 2010 13:15 "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <huduj8$1u3$1(a)fred.mathworks.com>... > sorry i made a mistake with my last question, here it is again. Hey guys I have a moving average code that I asked some advice on before, but i'm still not getting it to work any faster. It is still taking about 10 minutes to run even after adding in a line to preallocate my array. Here is the code I am using on a small example. > > a= 20041126 7.11 > 20041127 2.33 > 20041128 5.88 > 20041129 4.67 > 20041130 4.66 > 20041201 3.01 > 20041203 2.58 > 20041204 1.35 > > > MA=3 > n=length(a) > k=n - MA > m=1 > Mavg=zeros(1,n+1-MA) %attempt to preallocate the array > for q=1:n+1 - MA > j=m:MA+m - 1 > Mavg(q)=sum(a(j,2))/MA > m=m+1 > end > > the actuall data i use is 3000 columns of data and figuring a Moving Average of 150. This data of 8 columns and a Moveing average of 3 is just for an example to ask you guys some advice. I know from before people suggested using conv() and differencing my data and allocating an array. conv() im not quite understanding and i don't know what differencing the data is about, so I am attempting to preallocate an array with this line: Mavg=zeros(1,n+1-MA,1) , but still not working. > > Please some help to get me started with makeing this moving average calculate faster. > > thank you, > sean a hint: help filter; us
From: ImageAnalyst on 5 Jun 2010 15:20 Not really sure what your difficulty with conv() is. Perhaps this demo will help: % Generate sample data. a2DArray = rand(3000, 2); % Extract just the second column. column2 = a2DArray(:, 2); % Plot it. plot(column2, 'r-'); % Enlarge figure to full screen. set(gcf, 'Position', get(0,'Screensize')); % Get smoothing kernel for the convolution. kernel = ones(150,1)/150; % Do the smoothing. Key statement is here!!!!!! a_smoothed = conv(column2, kernel, 'same'); % Plot the smoothed signal. hold on; plot(a_smoothed, 'b-', 'LineWidth', 2); title('Original=red, smoothed = blue', 'FontSize', 24); % Elapsed time is 0.000525 seconds for the convolution.
From: Sean Douglas on 5 Jun 2010 16:58 ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <8945fd02-d4ab-4335-abd1-c8c3b92b4d55(a)z33g2000vbb.googlegroups.com>... > Not really sure what your difficulty with conv() is. Perhaps this > demo will help: > > % Generate sample data. > a2DArray = rand(3000, 2); > % Extract just the second column. > column2 = a2DArray(:, 2); > % Plot it. > plot(column2, 'r-'); > % Enlarge figure to full screen. > set(gcf, 'Position', get(0,'Screensize')); > % Get smoothing kernel for the convolution. > kernel = ones(150,1)/150; > > % Do the smoothing. Key statement is here!!!!!! > a_smoothed = conv(column2, kernel, 'same'); > > % Plot the smoothed signal. > hold on; > plot(a_smoothed, 'b-', 'LineWidth', 2); > title('Original=red, smoothed = blue', 'FontSize', 24); > > > % Elapsed time is 0.000525 seconds for the convolution. Image Analyst, Thank you, I am still a beginner at this stuff and it always helps to have a code to play around with, so thanks for sending me that well documented code. I am trying to fully understand it now. One thing i notice is that if i give it actualy data, (rather than random numbers) it then plots the data and the moving average exactly together, when it needs to plot the data 150 indexes ahead of the moving average so the two vectors correspond to each other. At least for stock data you have to do this. I am trying to figure out if conv() knows to do this. thank again
From: ImageAnalyst on 5 Jun 2010 17:33 Sean: Simply adjust your kernel: % Get smoothing kernel for the convolution. % Make it half zeros, a 1 for the center, and half 1's on the right. % Remember convolution flips the kernel prior to shifting and multiplying. % That's why the 1's are on the right (future) and not on the left (past). halfWindowWidth = 74; kernel = [zeros(halfWindowWidth,1); 1; ones(halfWindowWidth, 1)] / (halfWindowWidth + 1); % Kernel is now 149 long - 74 0's, a 1, and 75 1's for a total of 75 that will be averaged together.
|
Next
|
Last
Pages: 1 2 Prev: Colorize classes in an image Next: how can i open and close gui while code is executed |