function FMPMmod(action,s,ss,sss,ssss); % FMPMMOD Demonstrates modulation and demodulation. % % 02/13/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 = 'FMPMmod(''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 = 'FMPMmod(''setFs'')'; Fshndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','1000','Userdata',1000, ... 'Callback',callbackStr); %=================================== % Kf 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','Kf', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); %Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'FMPMmod(''setKf'')'; Kfhndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','0','Userdata',0, ... 'Callback',callbackStr); %=================================== % Kp 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','Kp', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); %Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'FMPMmod(''setKp'')'; Kphndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','20','Userdata',20, ... 'Callback',callbackStr); %==================================== % FM radio button btnTop = labelBottom-spacing; btnNumber=1; yPos=btnTop-(btnNumber-1)*(btnHt+spacing); labelStr='FM'; callbackStr='FMPMmod(''radio'',1,''FM'');'; % Generic button information btnPos=[left yPos-btnHt btnWid btnHt]; btn1Hndl=uicontrol( ... 'Style','radiobutton', ... 'Units','normalized', ... 'Position',btnPos, ... 'String',labelStr, ... 'value',1,'Userdata','FM', ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % PM radio button btnTop = labelBottom-spacing; btnNumber=2; yPos=btnTop-(btnNumber-1)*(btnHt+spacing); labelStr='PM'; callbackStr='FMPMmod(''radio'',2,''PM'');'; % 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='FMPMmod(''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 Kfhndl Kphndl ... ]; set(figNumber, ... 'Visible','on', ... 'UserData',hndlList); set(gcf,'Pointer','watch'); drawnow FMPMmod('changewave') set(gcf,'Pointer','arrow'); set(0,'ShowHiddenHandles',shh) return %====================================== elseif strcmp(action,'setFc'), hndlList=get(gcf,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = 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.'},... 'FMPMmod 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 Kf = get(Kfhndl,'UserData'); % kf, FM parameter Kp = get(Kphndl,'UserData'); % kp, PM parameter FMPMmod('changewave',Fc,Fs,Kf,Kp) elseif strcmp(action,'setFs'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = 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 Kp = get(Kphndl,'UserData'); % kp, PM parameter 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.'},... 'FMPMmod 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 Kf = get(Kfhndl,'UserData'); % kf, FM parameter Kp = get(Kphndl,'UserData'); % kp, PM parameter FMPMmod('changewave',Fc,Fs,Kf,Kp) return elseif strcmp(action,'setKf'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = hndlList(19); % Nhndl = hndlList(20); 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 Kf = get(Kfhndl,'UserData'); % kf, FM parameter Kp = get(Kphndl,'UserData'); % % N = get(Nhndl,'UserData'); % FMPMmod('changewave',Fc,Fs,Kf,Kp) return elseif strcmp(action,'setKp'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = 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 Kp must be less than'... 'thalf of the sampling frequency Fs.'},... 'FMPMmod 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 Kf = get(Kfhndl,'UserData'); % kf, FM parameter Kp = get(Kphndl,'UserData'); % kp, PM parameter FMPMmod('changewave',Fc,Fs,Kf,Kp) return elseif strcmp(action,'changewave') set(gcf,'Pointer','watch'); axHndl=gca; hndlList=get(gcf,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = hndlList(19); if nargin<2 Fc = 110; Fs = 1000; Kf = 40; Kp = 40; else Fc = s; Fs = ss; Kf = sss; Kp = ssss; end set(Fchndl,'string',num2str(Fc),'userdata',Fc); set(Fshndl,'string',num2str(Fs),'userdata',Fs); set(Kfhndl,'string',num2str(Kf),'userdata',Kf); set(Kphndl,'string',num2str(Kp),'userdata',Kp); FMPMmod('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 FMPMmod('modulate') return elseif strcmp(action,'modulate'), % modulate, demodulate, and update display set(gcf,'Pointer','watch'); axHndl=gca; hndlList=get(gcf,'Userdata'); mhndl = hndlList(1); %message signal mfhndl = hndlList(2); %FT of the message signal chndl = hndlList(3); %carrier signal cfhndl = hndlList(4); %FT of the carrier signal shndl = hndlList(5); %modulated signal sfhndl = hndlList(6); %FT of the modulated signal rhndl = hndlList(7); %Derivative of the mod. signal fhndl = hndlList(8);%Demodulated signal fohndl = hndlList(9); thndl = hndlList(10); Fchndl = hndlList(11); Fshndl = hndlList(12); btn1Hndl = hndlList(13); btn2Hndl = hndlList(14); Kfhndl = hndlList(18); Kphndl = 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 Kf = get(Kfhndl,'UserData'); % kf, FM parameter Kp = get(Kphndl,'UserData'); % kp, PM paramater method = get(btn1Hndl,'Userdata'); N = 2048; 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 %Integral of m int_m=zeros(1, length(t)); for i=1:length(t)-1, int_m(i+1)=int_m(i)+m(i)/(Fs-1); end 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 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; if strcmp(method,'FM') check = find((Fc + Kf.*m)<0); if isfinite(check) waitfor(msgbox({'Sorry, Fc+Kf*m(t) must be positive for all t.'... 'Some signals can be wrong.'},... 'FMPMmod Error','error','modal')) end n = int_m; %Fm modulated signal s = cos(2*pi*Fc*t+2*pi*Kf*int_m); SS = cos(2*pi*Fc*t+2*pi*Kp*n); %PM modulated signal of n=int_m. s_f= fft(s, N); s_f= fftshift (s_f); %Fourier Trans. of the fm modulated signal %derivative of the modulated signal diff_s = (2*pi*Fc+2*pi*Kf*m).*sin(2*pi*Fc*t+2*pi*Kf*int_m); %following portion is envelope detection FM rs = abs(diff_s); cm = rs(1); for i = 1:Fs if rs(i)