%Daniel Linnen %Project Program 5 %opening the file f1=fopen('C:\ELE498D\xn.dat', 'r'); %initializing arrays x=zeros(256,1); h=zeros(256,1); %declaration k=0; count=1; m=0; q0=0; q1=0; q2=0; q3=0; q4=0; %start of program while(count==1) %reading file [xn,count] = fread(f1, 1, 'short'); if(count==0) break; end %converting from integers xn = xn/32768; %insertion of random noise xn = xn + 0.01*rand; %update the input buffer for i=256:-1:2 x(i)=x(i-1); end x(1)=xn; %incrimenting counting variable k=k+1; %checking to see if %50 of the input is new if(k==128) %doing a 128 point DFT on the input q=fft(x,256); %checking for the maximums for i=1:128 %initializing the flags %these flags tell the program if a magnitude is already a %maximum so it doesnt record a maximum multiple times flag0=1; flag1=1; flag2=1; flag3=1; %calculating the magnitude of the current input mag=abs(real(q(i))); %doing comparisons and checking of flags if(mag>q0) flag0=0; q0=mag; qn0=i; end if(mag>q1 && flag0) flag1=0; q1=mag; qn1=i; end if(mag>q2 && flag0 && flag1) flag2=0; q2=mag; qn2=i; end if(mag>q3 && flag0 && flag1 && flag2) flag3=0; q3=mag; qn3=i; end if(mag>q4 && flag0 && flag1 && flag2 && flag3) q4=mag; qn4=i; end end %setting values other than the top 5 frequency bins to zero h=zeros(256,1); for i=1:128 if((i>=(qn0-2) && i<=(qn0+2)) || (i>=(qn1-2) && i<=(qn1+2)) || (i>=(qn2-2) && i<=(qn2+2)) || (i>=(qn3-2) && i<=(qn3+2)) || (i>=(qn4-2) && i<=(qn4+2))) h(i)=1; h(256-i)=1; else h(i)=0.001; h(256-i)=0.001; end end g=dot(q,h); %doing inverse of a 128 point DFT y1=ifft(g,256); %preparing output for plotting or listening for n=1:128 y(m+n)=y1(n+128); end m=m+128; %reinitialization of k k=0; end end