%Daniel Linnen %Project Program 4 clear all %opening the file f1=fopen('C:\ELE498D\xn.dat', 'r'); %initializing arrays x=zeros(256,1); %declaration k=0; count=1; m=0; q0=0; q1=0; q2=0; q3=0; q4=0; p=1; %start of program while(count==1) %reading files [xn,count] = fread(f1, 1, 'short'); if(count==0) break; end %converting from integers xn = xn/32768; %insertion of random noise xn = xn + 0.1*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,128); %checking for the maximums for z=1:64 %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(z))); %doing comparisons and checking of flags if(mag>q0) flag0=0; q0=mag; qn0=z; end if(mag>q1 && flag0) flag1=0; q1=mag; qn1=z; end if(mag>q2 && flag0 && flag1) flag2=0; q2=mag; qn2=z; end if(mag>q3 && flag0 && flag1 && flag2) flag3=0; q3=mag; qn3=z; end if(mag>q4 && flag0 && flag1 && flag2 && flag3) q4=mag; qn4=z; end end %setting values other than the top 5 frequency bins to zero for z=1:64 if(z==qn0 || z==qn1 || z==qn2 || z==qn3 || z==qn4) else q(z)=0; end end %reinitializing the values to zero q0=0; q1=0; q2=0; q3=0; q4=0; for z=65: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(z))); %doing comparisons and checking of flags if(mag>q0) flag0=0; q0=mag; qn0=z; end if(mag>q1 && flag0) flag1=0; q1=mag; qn1=z; end if(mag>q2 && flag0 && flag1) flag2=0; q2=mag; qn2=z; end if(mag>q3 && flag0 && flag1 && flag2) flag3=0; q3=mag; qn3=z; end if(mag>q4 && flag0 && flag1 && flag2 && flag3) q4=mag; qn4=z; end end %setting values other than the top 5 frequency bins to zero for z=65:128 if(z==qn0 || z==qn1 || z==qn2 || z==qn3 || z==qn4) else q(z)=0; end end %doing inverse of a 128 point DFT y1=ifft(q,128); %preparing output for plotting or listening for n=1:128 y(m+n)=y1(n); p=p+1; end m=m+128; %reinitialization of k k=0; end end