# Shifting / Permutation of an array taking a long time [closed]

I'm using matlab/octave and would like some help trying to improve the speed of my code. I've tried to vectorize the code but I run out of memory so I went back to for loops. What I'm trying to do is have a function that will allow me to shift an array cell by cell. The reason I'm doing this is to have exact precision over a signal. Once I shift the array I plan on adding several of them together and comparing them to the original signal.

I created a function along with some test code to test the speed. See times and code below

ii = 0 -elapsed time -67.5671sec- or -1.1261mins- or -0.0188hours-

ii = 14700 -elapsed time -613.3785sec- or -10.2230mins- or -0.1704hours-

ii = 29400 -elapsed time -1150.6487sec- or -19.1775mins- or -0.3196hours-

The code takes a long time to run on 1 second of an audio signal at a sample rate of 44100 any idea how I can speed this up and accomplish my goals?

Function:

function [array_shifted]=rtphaseshiftarraypts(sigarray,pts_to_shift)
fs=length(sigarray);

a_L=zeros(1,length(sigarray));

cycle_permute_lt = @(sigarray, k) [sigarray(mod((1:end)-k-1, end)+1 )];

for ii=0:1:fs
a_L=cycle_permute_lt(sigarray, ii+pts_to_shift);
end;
array_shifted=a_L;
end


Test Code:

clear all, clc,clf,tic
fs=44100;
t=linspace(0,2*pi,fs);
y=sinc(t*1);
y=fliplr(y); %just flipping y to look like milo wolffs
[y]=rtphaseshiftarraypts(y,fs/2);

lr_sig=[y*1 -y*1];
for ii=0:round(fs/3):fs %ii=0:1:90 going right , ii=0:-1:-90 going left

array_shifted_L=y*1; %ampcomb(ii);
array_shifted_R=-y*1; %ampcomb(ii);
ii
fprintf('\n-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);

acombr=rtphaseshiftarraypts(lr_sig,ii);
acombl=rtphaseshiftarraypts(lr_sig,-ii+fs/2);

subplot(4,1,1),plot(array_shifted_L)
ylabel('rgt-in-wave')
axis([0 fs+100 -2 2])

subplot(4,1,2),plot(array_shifted_R)
ylabel('lft-out-wave')
axis([0 fs+100 -2 2])

subplot(4,1,3),plot(acombr)
axis([0 (fs*2)+100 -2 2])

subplot(4,1,4),plot(acombl)
axis([0 (fs*2)+100 -2 2])
str_title='Angle is ';
title([str_title,num2str(ii),' pts']) %turns of latex interperter so I can use underscores my way

pause(.001)
%hold on
end
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);

• You seem to be incredibly confused about what you are trying to achieve... at least your matlab code incredibly confused me. Can you give a numeric (concrete) example using a size 10 array that shows what rtphaseshiftarraypts is trying to achieve? – Peter K. Feb 12 '13 at 20:23
• @Peter K Sure if I have an array with 10 values 1,2,3,4,5,6,7,8,9,10 and I shift the array over three I would get 4,5,6,7,8,9,10,1,2,3 basically it's doing a permutation – Rick T Feb 12 '13 at 20:25
• This is a programming question, not a signal processing question, and I vote to close. – Dilip Sarwate Feb 12 '13 at 20:44
• @DilipSarwate: Agreed... though I've only just gained the voting rights! :-) – Peter K. Feb 13 '13 at 13:38

I would just follow this example and use circshift, or that more detailed solution for multi-dimensional arrays.