function ammod(action,s,ss,sss,ssss); % AMMOD Demonstrates modulation and demodulation. % % 03/26/2003 Polytechnic University if nargin<1, action='initialize'; end; if strcmp(action,'initialize'), shh = get(0,'ShowHiddenHandles'); set(0,'ShowHiddenHandles','on'); figNumber=figure( ... 'Name','Modulation/Demodulation Demo', ... 'handlevisibility','callback',... 'IntegerHandle','off',... 'NumberTitle','off'); %to accomodate labels in the figure figpos = get(figNumber,'Position'); set(figNumber,'Position',[figpos(1) figpos(2)-160 figpos(3)+150 figpos(4)+180]); %================================= % Set up the axes mhndl = axes( ... 'Units','normalized', ... 'Position',[0.05 0.79 0.35 0.17], ... 'XTick',[],'YTick',[], ... 'Box','on'); mfhndl = axes( ... 'Units','normalized', ... 'Position',[0.45 0.79 0.35 0.17], ... 'XTick',[],'YTick',[], ... 'Box','on'); chndl = axes( ... 'Units','normalized', ... 'Position',[0.05 0.60 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); cfhndl = axes( ... 'Units','normalized', ... 'Position',[0.45 0.60 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); shndl = axes( ... 'Units','normalized', ... 'Position',[0.05 0.41 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); sfhndl = axes( ... 'Units','normalized', ... 'Position',[0.45 0.41 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); rhndl = axes( ... 'Units','normalized', ... 'Position',[0.05 0.22 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); fhndl = axes( ... 'Units','normalized', ... 'Position',[0.45 0.22 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); fohndl = axes( ... 'Units','normalized', ... 'Position',[0.05 0.03 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); thndl = axes( ... 'Units','normalized', ... 'Position',[0.45 0.03 0.35 0.16], ... 'XTick',[],'YTick',[], ... 'Box','on'); %==================================== % Information for all buttons (and menus) labelColor=[0.3 0.8 0.5]; yInitPos=0.90; menutop=0.95; btnTop = 0.6; top=0.75; left=0.83; btnWid=0.14; btnHt=0.06; textHeight = 0.05; textWidth = 0.10; spacing=0.012; %==================================== % The CONSOLE frame frmBorder=0.019; frmBottom=0.05; frmHeight = 0.895; frmWidth = btnWid; yPos=frmBottom-frmBorder; frmPos=[left-frmBorder yPos frmWidth+2*frmBorder frmHeight+2*frmBorder]; h=uicontrol( ... 'Style','frame', ... 'Units','normalized', ... 'Position',frmPos, ... 'BackgroundColor',[0.1 0.15 0.25]); yPos=menutop; %=================================== % Carrier frequency label and text field top = yPos - btnHt - spacing; labelWidth = frmWidth-textWidth-.01; labelBottom = top-textHeight; labelLeft = left; labelPos = [labelLeft labelBottom labelWidth textHeight]; h = uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',labelPos, ... 'HorizontalAlignment','left', ... 'String','Fc', ... 'Interruptible','off', ... 'BackgroundColor',[0.15 0.75 0.5], ... 'ForegroundColor','white'); % Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'ammod(''setFc'')'; FcHndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','400','Userdata',400, ... 'callback',callbackStr); %=================================== % Sampling frequency label and text field labelBottom=top-2*textHeight-spacing; labelLeft = left; labelPos = [labelLeft labelBottom labelWidth textHeight]; h = uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',labelPos, ... 'String','Fs', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); % Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'ammod(''setFs'')'; FsHndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','1000','Userdata',1000, ... 'Callback',callbackStr); %=================================== % Phase difference label and text field labelBottom=top-3*textHeight-2*spacing; labelLeft = left; labelPos = [labelLeft labelBottom labelWidth textHeight]; h = uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',labelPos, ... 'String','Ph', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); % Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'ammod(''setPh'')'; Phndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','0','Userdata',0, ... 'Callback',callbackStr); %=================================== % Cut_off frequency label and text field labelBottom=top-4*textHeight-3*spacing; labelLeft = left; labelPos = [labelLeft labelBottom labelWidth textHeight]; h = uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',labelPos, ... 'String','Co', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); % Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'ammod(''setCo'')'; Cohndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','20','Userdata',20, ... 'Callback',callbackStr); %==================================== % AM radio button btnTop = labelBottom-spacing; btnNumber=1; yPos=btnTop-(btnNumber-1)*(btnHt+spacing); labelStr='AM'; callbackStr='ammod(''radio'',1,''am'');'; % Generic button information btnPos=[left yPos-btnHt btnWid btnHt]; btn1Hndl=uicontrol( ... 'Style','radiobutton', ... 'Units','normalized', ... 'Position',btnPos, ... 'String',labelStr, ... 'value',1,'Userdata','am', ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % Conventional AM radio button btnTop = labelBottom-spacing; btnNumber=2; yPos=btnTop-(btnNumber-1)*(btnHt+spacing); labelStr='AMcon'; callbackStr='ammod(''radio'',2,''amcon'');'; % Generic button information btnPos=[left yPos-btnHt btnWid btnHt]; btn2Hndl=uicontrol( ... 'Style','radiobutton', ... 'Units','normalized', ... 'Position',btnPos, ... 'String',labelStr, ... 'value',0, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % Message box in center of figure messageHndl = uicontrol('style','edit',... 'string','Resampling speech waveform ...',... 'units','normalized',... 'position',[.15 .45 .5 .15],... 'max',2,... 'visible','off'); %==================================== % The INFO button labelStr='Info'; callbackStr='ammod(''info'')'; helpHndl=uicontrol( ... 'Style','pushbutton', ... 'Units','normalized', ... 'Position',[left frmBottom+btnHt+spacing btnWid btnHt], ... 'String',labelStr, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % The CLOSE button labelStr='Close'; callbackStr='close(gcf)'; closeHndl=uicontrol( ... 'Style','pushbutton', ... 'Units','normalized', ... 'Position',[left frmBottom btnWid btnHt], ... 'String',labelStr, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); hndlList=[mhndl mfhndl chndl cfhndl ... shndl sfhndl rhndl fhndl fohndl thndl ... FcHndl FsHndl btn1Hndl btn2Hndl ... messageHndl helpHndl closeHndl Phndl Cohndl]; set(figNumber, ... 'Visible','on', ... 'UserData',hndlList); set(gcf,'Pointer','watch'); drawnow ammod('changewave') set(gcf,'Pointer','arrow'); set(0,'ShowHiddenHandles',shh) return elseif strcmp(action,'setFc'), hndlList=get(gcf,'Userdata'); %filtHndl = hndlList(12); FcHndl = hndlList(11); FsHndl = hndlList(12); Phndl = hndlList(18); Cohndl = hndlList(19); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); Fs = get(FsHndl,'UserData'); if vv<0, vv = v; end vv = round(vv); if vv>=Fs/2 waitfor(msgbox({'Sorry, the carrier frequency Fc must be less than'... 'half the sampling frequency Fs.'},... 'ammod Error','error','modal')) vv = v; end set(gco,'Userdata',vv,'String',num2str(vv)) if vv == v return end Fs = get(FsHndl,'UserData'); % Sampling frequency Fc = get(FcHndl,'UserData'); % Carrier frequency Ph = get(Phndl,'UserData'); % Phase difference Co = get(Cohndl,'UserData'); % Cut_off frequency ammod('changewave',Fc,Fs,Ph,Co) elseif strcmp(action,'setFs'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); FcHndl = hndlList(11); FsHndl = hndlList(12); Phndl = hndlList(18); Cohndl = hndlList(19); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); if vv<=0, vv = v; end Fc = get(FcHndl,'UserData'); % Carrier frequency Co = get(Cohndl,'UserData'); % Cut_off frequency vv = round(vv*10)/10; if Fc>=vv/2 waitfor(msgbox({'Sorry, the sampling frequency Fs must be more than'... 'twice the carrier frequency Fc.'},... 'ammod Error','error','modal')) vv = v; end if Co>=vv/2 waitfor(msgbox({'Sorry, the sampling frequency Fs must be more than'... 'twice the cut_off frequency Co.'},... 'ammod Error','error','modal')) vv = v; end set(gco,'Userdata',vv,'String',num2str(vv)) if vv == v set(fig,'Pointer','arrow'); return end Fs = get(FsHndl,'UserData'); % Sampling frequency Fc = get(FcHndl,'UserData'); % Carrier frequency Ph = get(Phndl,'UserData'); % Phase difference Co = get(Cohndl,'UserData'); % Cut_off frequency ammod('changewave',Fc,Fs,Ph,Co) return elseif strcmp(action,'setPh'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); FcHndl = hndlList(11); FsHndl = hndlList(12); Phndl = hndlList(18); Cohndl = hndlList(19); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); vv = round(vv*10)/10; set(gco,'Userdata',vv,'String',num2str(vv)) if vv == v set(fig,'Pointer','arrow'); return end Fs = get(FsHndl,'UserData'); % Sampling frequency Fc = get(FcHndl,'UserData'); % Carrier frequency Ph = get(Phndl,'UserData'); % Phase difference Co = get(Cohndl,'UserData'); % Cut_off frequency ammod('changewave',Fc,Fs,Ph,Co) return elseif strcmp(action,'setCo'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); FcHndl = hndlList(11); FsHndl = hndlList(12); Phndl = hndlList(18); Cohndl = hndlList(19); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); Fs = get(FsHndl,'UserData'); % Carrier frequency vv = round(vv*10)/10; if Fs<=vv*2 waitfor(msgbox({'Sorry, the cut_off frequency Co must be less than'... 'thalf of the sampling frequency Fs.'},... 'ammod Error','error','modal')) vv = v; end set(gco,'Userdata',vv,'String',num2str(vv)) if vv == v set(fig,'Pointer','arrow'); return end Fs = get(FsHndl,'UserData'); % Sampling frequency Fc = get(FcHndl,'UserData'); % Carrier frequency Ph = get(Phndl,'UserData'); % Phase difference Co = get(Cohndl,'UserData'); % Cut_off frequency ammod('changewave',Fc,Fs,Ph,Co) return elseif strcmp(action,'changewave') set(gcf,'Pointer','watch'); axHndl=gca; hndlList=get(gcf,'Userdata'); FcHndl = hndlList(11); FsHndl = hndlList(12); Phndl = hndlList(18); Cohndl = hndlList(19); if nargin<2 Fc = 400; Fs = 1000; Ph = 0; Co = 20; else Fc = s; Fs = ss; Ph = sss; Co = ssss; end set(FcHndl,'string',num2str(Fc),'userdata',Fc); set(FsHndl,'string',num2str(Fs),'userdata',Fs); set(Phndl,'string',num2str(Ph),'userdata',Ph); set(Cohndl,'string',num2str(Co),'userdata',Co); ammod('modulate') elseif strcmp(action,'radio'), axHndl=gca; hndlList=get(gcf,'Userdata'); for i=13:14, set(hndlList(i),'value',0) % Disable all the buttons end set(hndlList(s+12),'value',1) % Enable selected button set(hndlList(13),'Userdata',ss) % Remember selected button ammod('modulate') return elseif strcmp(action,'modulate'), % modulate, demodulate, and update display set(gcf,'Pointer','watch'); axHndl=gca; hndlList=get(gcf,'Userdata'); mhndl = hndlList(1); mfhndl = hndlList(2); chndl = hndlList(3); cfhndl = hndlList(4); shndl = hndlList(5); sfhndl = hndlList(6); rhndl = hndlList(7); fhndl = hndlList(8); fohndl = hndlList(9); thndl = hndlList(10); FcHndl = hndlList(11); FsHndl = hndlList(12); btn1Hndl = hndlList(13); btn2Hndl = hndlList(14); Phndl = hndlList(18); Cohndl = hndlList(19); set(gcf,'nextplot','add') edgecolor = get(gca,'colororder'); edgecolor = edgecolor(1,:); Fs = get(FsHndl,'UserData'); % Sampling frequency Fc = get(FcHndl,'UserData'); % Carrier frequency Ph = get(Phndl,'UserData'); % Phase difference Co = get(Cohndl,'UserData'); % Cut_off frequency method = get(btn1Hndl,'Userdata'); t= linspace(0,1,Fs) ; m= ones(1, Fs); m= [m(1:round(.4*Fs))*1 m(round(.4*Fs)+1:round(.7*Fs))*-2 m(round(.7*Fs)+1:Fs)*0]; %signal I %m= sinc (4*t); %signal II %m= cos(2*pi*10*t); %signal III c= cos(2*pi*Fc*t); %carrier signal s= m .* c; %modulated signal a= 0.50; %modulation index for conventional AM case if strcmp(method,'amcon') m_n = m /max(abs(m)); %normalized message signal for AMcon s = (1+a*m_n).*c ; %modulated signal for AMcon end N= 512; m_f= fft(m, N); m_f= fftshift (m_f); %Fourier Trans. of the message signal c_f= fft(c, N); c_f= fftshift (c_f); %Fourier Trans. of the carrier signal s_f= fft(s, N); s_f= fftshift (s_f); %Fourier Trans. of the modulated signal dt_fft= 1/Fs; df_fft= 1/(N*dt_fft); x_f1= ((N/2+1):N)-(N+1); x_f2= (1:(N/2))-1; x_f= [x_f1 x_f2]*df_fft; r= s.*(cos(2*pi*Fc*t+Ph)); %input to the filter r_f= fft(r,N); r_f= fftshift (r_f); %filter input in frequency domain Nd=5; Wn=Co/(Fs/2); [B, A]= butter(Nd, Wn); [Fw, ww]= freqz(B,A,N,'whole'); Fw= fftshift(Fw); %filter frequency response to= 2*filter(B,A,r); %output in time domain fo= fft(to,N); fo= fftshift(fo); %output in fequency domain %following portion is envelope detection for AMcon if strcmp(method,'amcon') rs = abs(s); cm = rs(1); for i = 1:Fs if rs(i)