[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FFT and changing frequency and vectorizing for loop
From: 
Rick T 
Subject: 
FFT and changing frequency and vectorizing for loop 
Date: 
Sat, 2 Apr 2011 12:02:32 1000 
FFT and changing frequency and vectorizing for loop
Greetings All
I can increase and decrease the frequency of the
signal using the combination of fft and a series expansion for loop in the code below
but if the signal/array is to large it becomes extremely
slow (an array that's 1x44100 takes about 2 mins to complete) I'm sure it has to do with the for loop but
I'm not exactly sure how to vectorize them to improve performance
Any recommendations
tia sal22
%create signal
clear all, clc,clf,tic
x= linspace(0,2*pi,44100)';
%Used in exporting to ycalc audio file make sure in sync with above
freq_orig=1;
freq_new=4
vertoff=0;
vertoffConj=0;
vertoffInv=0;
vertoffInvConj=0;
phaseshift=(0)*pi/180 ; %can use mod to limit to 180 degrees
y=sin(freq_orig*(x));
[size_r,size_c]=size(y);
N=size_r; %to test make 50
T=2*pi;
dt=T/N;
t=linspace(0,Tdt,N)';
phase = 0;
f0 = 1/T; % Exactly, one period
y=(y/max(abs(y))*.8)/2; %make the max amplitude here
C = fft(y)/N; % No semicolon to display output
A = real(C);
B = imag(C)*1; %I needed to multiply by 1 to get the correct sign
% SingleSided (f >= 0)
An = [A(1); 2*A(2:round(N/2)); A(round(N/2)+1)]; %needed to put the ' to get vaules in rows
Bn = [B(1); 2*B(2:round(N/2)); B(round(N/2)+1)]; %needed to put the ' to get vaules in ro
pmax=N/2;
ycalc=zeros(N,1); %preallocating space for ycalc
w=0;
for p=2:pmax
%
%%1 step) recreate signal using equation
ycalc=ycalc+An(p)*cos(freq_new*(p1).*tphaseshift)+Bn(p)*sin(freq_new*(p1).*tphaseshift)+(vertoff/pmax);
w=w+(360/(pmax1)); %used to create phaseshift
phaseshift=w;
end;
fprintf('\n Completed in %4.4fsec or %4.4fmins\n',toc,toc/60);
subplot(2,1,1), plot(y),title('Orginal Signal');
subplot(2,1,2),plot(ycalc),title('FFT new signal');


 FFT and changing frequency and vectorizing for loop,
Rick T <=