How to prevent Signal drifting when changing frequency

When I recreate a signal it seems to look great. But when I try and increase it's frequency (which is done in a for loop and the variable new_freq=2) it starts to drift.

See Image below (The top image is 1hz and the bottom image is 2hz. I circled the image with the problem). I also noticed when I increase the sample frequency the problem gets even more noticeable. Notice the top one is at 8000 as the sample frequency and the bottom one is at sample frequency of 44100.

I've posted the for loop below any ideas why the variables aa_sig_rebuilt_norm_bin and aa_sig_combined_norm_bin are drifting so much? Please note I'm using much more complex signals than just sine waves (if I were just using sine waves I would use the sinwave equation to increase the frequency. I used a sinewave as an example)

The whole for loop is base on rebuilding a signal using the equation AmplitudeCos(freqtime+phase) I did an fft/ifft to get all the frequencies/amplitudes/phases then just change the frequency to what I want

I do want to vectorize this for loop but at the moment I'm trying to fix the drift

new_freq=[2]; %increase frequency
fn_array=[];

t_rebuilt=linspace(0,2*pi,fs);

for zz=1:1:length(new_freq); %

zz
%needs to be in for loop to reset variables for next new_freq
aa_sig_rebuilt_norm=zeros(1,length(t_rebuilt));
aa_sig_rebuilt_norm_bin=zeros(1,length(t_rebuilt));
aa_sig_combined_norm=zeros(1,length(t_rebuilt));
aa_sig_combined_norm_bin=zeros(1,length(t_rebuilt));
sig_norm=zeros(1,length(t_rebuilt));
sig_norm_bin=zeros(1,length(t_rebuilt));
for kk=1:1:length(xfreq_orig);
aa_sig_rebuilt_norm = array1(kk, 2)*cos (array1(kk,1)*t_rebuilt+(array1(kk, 4)));
aa_sig_combined_norm = aa_sig_combined_norm + aa_sig_rebuilt_norm;

aa_sig_rebuilt_norm_bin = array1(kk, 2)*cos ((new_freq(zz,1))*array1(kk,1)*t_rebuilt+(array1(kk, 4))); %binural section
aa_sig_combined_norm_bin = aa_sig_combined_norm_bin + aa_sig_rebuilt_norm_bin;
end;

%have it export to file
sig_norm=(aa_sig_combined_norm/max(abs(aa_sig_combined_norm))*.8); %normalize signal
sig_norm_bin=(aa_sig_combined_norm_bin/max(abs(aa_sig_combined_norm_bin))*.8); %normalize signal

wavwrite([sig_norm' sig_norm_bin'] ,fs,16,strcat(dirpathtmp,fn_LR_norm_bin)); %export file

end;


• This is a programming question and not a signal processing question. I vote to close it as off-topic for this site. – Dilip Sarwate Apr 27 '13 at 11:55
• What do you mean by "increase it's frequency with a for loop?" What are you actually trying to do to the signal? It's not clear from a brief look at your code dump. It would be most useful if you provided some more details including a self-contained runnable example. – Jason R Apr 27 '13 at 11:55
• @Jason R when I mean "increase frequency with a for loop" I should have just said increase frequency. So if a vocal signal is defined as 1hz if I put a 2 in the variable new_freq=[2] then it will double it's frequency. The code uses multiple functions and one guy already wants to close this question for posting a small for loop that deals with creating the signal and pictures to help explain things why, I'm not sure, so adding more code will just piss him off even more. – Rick T Apr 27 '13 at 12:09
• @RickT: Dilip brings up a good point. Your question as currently posed isn't a very good one. Instead of asking how to fix your implementation, a question more along the lines of how to appropriately shift the pitch of a voice signal would be better. – Jason R Apr 27 '13 at 16:40
• I don't have an answer for that. You would be better off posting a new question that is based around that query, as it will garner more attention by those who have expertise in that area. – Jason R Apr 27 '13 at 17:44