% ### BuildClick.m ### 10.04.13 % code to visually build up a click by successively adding higher and % higher frequency clear; clf; % -------------------------------- % User Inputs SR= 44100; % sample rate [Hz] Npoints= 8192; % length of fft window (# of points) [should ideally be 2^N] % [time window will be the same length] % -------------------------------- dt= 1/SR; % spacing of time steps % create a freq. array (for FFT bin labeling) freq= [0:Npoints/2]; freq= SR*freq./Npoints; % quantize the freq. (so to have an integral # of cycles) % [see quantizeF.m for further aspects on this] df = SR/Npoints; % create an array of time points, Npoints long %t= linspace(0,Npoints/SR,Npoints); t=[0:1/SR:(Npoints-1)/SR]; % click CLKon= 1000; % index at which click turns 'on' (starts at 1) CLKoff= 1001; % index at which click turns 'off' clktemp1= zeros(1,Npoints); clktemp2= ones(1,CLKoff-CLKon); signal= [clktemp1(1:CLKon-1) clktemp2 clktemp1(CLKoff:end)]; % ------------------------------ % ******* % plot time waveform of signal if 1==1 figure(1); clf plot(t*1000,signal,'k.-','MarkerSize',5) grid on; hold on; xlabel('Time [ms]') ylabel('Signal') title('Time Waveform') end % ******* % now plot rfft of the signal sigSPEC= rfft(signal); % MAGNITUDE figure(2); clf; subplot(211) plot(freq/1000,db(sigSPEC),'ko-','MarkerSize',3) hold on; grid on; ylabel('Magnitude [dB]') title('Spectrum') % PHASE subplot(212) % radians (wrapped) %plot(freq,angle(rfft(signal)),'o-','MarkerSize',3) % cycles (unwrapped) plot(freq/1000,cycs(sigSPEC),'ko-','MarkerSize',3) xlabel('Frequency [kHz]') ylabel('Phase [cycles]') grid on; %axis([0 2500 -350 10]) % ******* % now make animation of click getting built up sum= zeros(1,numel(t)); figure(3); clf; for nn=1:numel(freq) sum= sum+ abs(sigSPEC(nn))*cos(2*pi*freq(nn)*t + angle(sigSPEC(nn))); plot(t,sum) legend(['Highest freq= ',num2str(freq(nn)/1000),' kHz']) pause(0.2) end