function ts_tutorial_v1 clear all close all sr1 = 1000; sr2 = 128; tmax = 1; iopt = 1; i_wf = menu('Choose waveform:','Single 8 Hz', '1 and 16 Hz','1 and 20 Hz') % Define the input signal (sum of 3 sine curves) if i_wf == 1; nf =1 ; a = [1,0.0,0.0]; ph = [0., 0.,0.,]; f = [8,10,64]; end if i_wf == 2; nf = 2; a = [1,0.5,0.0]; ph = [0., 0.,0.,]; f = [1,16,64]; end if i_wf == 3; nf = 2; a = [1,0.5,0.0]; ph = [0., 0.,0.,]; f = [1,20,64]; end w = 2*pi*f; % Amplifier and ADC characteristics vmax = 2; vmin = -2; gain = 1; vmax_plot=[vmax,vmax]; vmin_plot=[vmin,vmin]; t_plot = [0,tmax] nbit = 2; while iopt < 8; dv = (vmax-vmin)/nbit; t1=[0.0:1/sr1:tmax];n1 = length(t1) for it=1:n1 v1(it)= a(1)*sin(w(1)*t1(it)+ph(1))+a(2)*sin(w(2)*t1(it)+ph(2))+a(3)*sin(w(3)*t1(it)+ph(3)); end t2=[0.0:1/sr2:tmax];n2 = length(t2) for it=1:n2 v2(it)= (a(1)*sin(w(1)*t2(it)+ph(1))+a(2)*sin(w(2)*t2(it)+ph(2))+a(3)*sin(w(3)*t2(it)+ph(3)))*gain; ndig = fix(v2(it)/dv); v2(it) = ndig*dv; if v2(it) >= vmax; v2(it) = vmax; end if v2(it) <= vmin; v2(it) = vmin; end end subplot(2,1,1) plot(t1,v1); axis([0,tmax,vmin*1.3,vmax*1.3]) %title(['True signal : f = ',num2str(f(1)),' Hz']); if nf == 1; title(['True signal : f_1=',num2str(f(1)),' Hz']) end if nf == 2; title(['True signal : f_1=',num2str(f(1)),' Hz; f_2=',num2str(f(2)),'Hz']) end ylabel('signal (V)') subplot(2,1,2) plot(t2,v2,'.-') title('Recorded data') title(['Recorded signal : Gain = ',num2str(gain),'; Sample rate = ', num2str(sr2),' Hz; Digitization level = ', num2str(nbit+1)]) xlabel('time(s)') ylabel('signal (V)') hold on plot(t_plot,vmax_plot,'r'); plot(t_plot,vmin_plot,'r'); axis([0,tmax,vmin*1.3,vmax*1.3]) for idig = 0:nbit digplot = [(idig-nbit/2)*dv, (idig-nbit/2)*dv] tplot =[0,tmax] plot(tplot,digplot,':') end hold off iopt = menu('Change paramters:','SR x2','SR x 0.5','bit x 2','bit x 0.5','gain + 0.1','gain - 0.1', 'phase +45 deg','quit') if iopt ==1; sr2 = sr2*2; end; if iopt ==2; sr2 = sr2*0.5; end; if iopt ==3; nbit = nbit*2; end; if iopt ==4; nbit = nbit/2; end; if iopt ==5; gain = gain+0.1; end; if iopt ==6; gain = gain-0.1; end; if iopt ==7; ph(1) = ph(1)+pi/4; end; clear v2; t2; end print -djpeg100 tsfig.jpg end