필터 지우기
필터 지우기

xcor and rms vectorization!

조회 수: 5 (최근 30일)
Ehsan
Ehsan 2012년 11월 30일
I'm really hitting the wall trying to optimize my code by vectorization! In my application, I have ‘data’ arrays that are 200,000 x 2 in size. I have to perform cross correlation (xcorr) on small time windows (200 elements long) on the data. Basically, I loop through the 'data' array and read 200 elements at once, process it, and move on to the next 200 elements. This code is very slow and I need to speed it up. I wonder if there is a way I could vectorize this operation (xcorr) so that I could perform the whole thing in one shot (i.e. do 1000 xcorr operations on 1000 arrays of 200x2 size).
I also have many more chunks of code like this where I have to read small chunks of an array in a loop, process it and then move on to the next chunk. I wonder if there's a better way of doing that (for example, for the scenario described above, I'm also calculating the rms value of the data segment that's processed in each iteration of the loop)! Any advice would be much appreciated.
Thanks!
% 'data' contains 200000x2 elements The pseudo-code looks like this
num_windows = 1000;
segment_size = length(data) / num_widnows;
start_index = 1;
end_index = segment_size;
segment = zeros(segment_size,2);
for i=1:num_windows
for j=1:2
segment(:,j) = data(start_index:end_index,j);
end
[c,lags] = xcorr(segment(:,1), segment(:,2), coeff);
rms_array = rms(segment);
% copy the results as necessary …
start_index = end_ndex + 1;
end_index = end_index + segment_size;
end

답변 (1개)

dpb
dpb 2012년 11월 30일
Well, first of all, you don't need to copy the original data...
num_windows = 1000; segment_size = length(data) / num_wimdows; i1 = 1; i2 = segment_size;
for i=1:num_windows [c,lags] = xcorr(data(i1:i2,1), data(i1:i2,2), ‘coeff’); r = rms(data(i1:i2,1:2));
% copy the results as necessary …
Probably don't need to copy here, either--preallocate the outputs desired to keep and index into those arrays, too...
i1 = i2+1;
i2 = i2+segment_size;
end
I also shortened temporary variable names drastically to aid in legibility of the indices...

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by