function FMPMnoise(action,s,ss,sss,ssss,sssss); % FMPMnoise 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 = 'FMPMnoise(''setFc'')'; Fchndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','110','Userdata',110, ... '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 = 'FMPMnoise(''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 = 'FMPMnoise(''setKf'')'; Kfhndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','40','Userdata',40, ... '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 = 'FMPMnoise(''setKp'')'; Kphndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','40','Userdata',40, ... 'Callback',callbackStr); %=================================== % SNR label and text field labelBottom=top-5*textHeight-4*spacing; labelLeft = left; labelPos = [labelLeft labelBottom labelWidth textHeight]; h = uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',labelPos, ... 'String','SNR', ... 'HorizontalAlignment','left', ... 'Interruptible','off', ... 'Background',[0.15 0.75 0.5], ... 'Foreground','white'); % Text field textPos = [labelLeft+labelWidth labelBottom textWidth textHeight]; callbackStr = 'FMPMnoise(''setSNR'')'; SNRhndl = uicontrol( ... 'Style','edit', ... 'Units','normalized', ... 'Position',textPos, ... 'HorizontalAlignment','center', ... 'Background','white', ... 'Foreground','black', ... 'String','10','Userdata',10, ... 'Callback',callbackStr); %==================================== % FM radio button btnTop = labelBottom-spacing; btnNumber=1; yPos=btnTop-(btnNumber-1)*(btnHt+spacing); labelStr='FM'; callbackStr='FMPMnoise(''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='FMPMnoise(''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 PlayDemod button labelStr='Play demodulated'; callbackStr='FMPMnoise(''playsound'',3)'; helpHndl=uicontrol( ... 'Style','pushbutton', ... 'Units','normalized', ... 'Position',[left frmBottom+2*btnHt+2*spacing btnWid btnHt], ... 'String',labelStr, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % The PlayReceived button labelStr='Play received'; callbackStr='FMPMnoise(''playsound'',2)'; helpHndl=uicontrol( ... 'Style','pushbutton', ... 'Units','normalized', ... 'Position',[left frmBottom+3*btnHt+3*spacing btnWid btnHt], ... 'String',labelStr, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % The PlayOrig button labelStr='Play message'; callbackStr='FMPMnoise(''playsound'',1)'; helpHndl=uicontrol( ... 'Style','pushbutton', ... 'Units','normalized', ... 'Position',[left frmBottom+4*btnHt+4*spacing btnWid btnHt], ... 'String',labelStr, ... 'BackgroundColor',[0.15 0.75 0.5],... 'Callback',callbackStr); %==================================== % The INFO button labelStr='Info'; callbackStr='FMPMnoise(''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 ... SNRhndl]; set(figNumber, ... 'Visible','on', ... 'UserData',hndlList); set(gcf,'Pointer','watch'); drawnow FMPMnoise('changewave') set(gcf,'Pointer','arrow'); set(0,'ShowHiddenHandles',shh) return %====================================== elseif strcmp(action,'setFc'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); Fchndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = hndlList(19); SNRhndl = hndlList(20); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); Fs = get(Fshndl,'UserData'); Fc = get(Fchndl,'UserData'); % Carrier frequency 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.'},... 'FMPMnoise 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 SNR = get(SNRhndl,'UserData'); FMPMnoise('changewave',Fc,Fs,Kf,Kp,SNR) 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); SNRhndl = hndlList(20); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); if vv<=0, vv = v; 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 SNR = get(SNRhndl,'UserData'); 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.'},... 'FMPMnoise 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 SNR = get(SNRhndl,'UserData'); FMPMnoise('changewave',Fc,Fs,Kf,Kp,SNR) 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); SNRhndl = 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'); % SNR = get(SNRhndl,'UserData'); FMPMnoise('changewave',Fc,Fs,Kf,Kp,SNR) 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); SNRhndl = hndlList(20); messageHndl = hndlList(15); v = get(gco,'Userdata'); s = get(gco,'String'); vv = eval(s,num2str(v)); 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 SNR = get(SNRhndl,'UserData'); 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.'},... % 'FMPMnoise 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 SNR = get(SNRhndl,'UserData'); FMPMnoise('changewave',Fc,Fs,Kf,Kp,SNR) return elseif strcmp(action,'setSNR'), fig = gcf; set(fig,'Pointer','watch'); hndlList=get(fig,'Userdata'); FcHndl = hndlList(11); Fshndl = hndlList(12); Kfhndl = hndlList(18); Kphndl = hndlList(19); SNRhndl = hndlList(20); 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; 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 SNR = get(SNRhndl,'UserData'); % SNR FMPMnoise('changewave',Fc,Fs,Kf,Kp,SNR) 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); SNRhndl = hndlList(20); if nargin<2 Fc = 110; Fs = 1000; Kf = 40; Kp = 40; SNR = 10; else Fc = s; Fs = ss; Kf = sss; Kp = ssss; SNR = sssss; 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); set(SNRhndl,'string',num2str(SNR),'userdata',SNR); FMPMnoise('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 FMPMnoise('modulate') return elseif strcmp(action,'playsound'), sound = s; set(gcf,'Pointer','watch'); axHndl=gca; hndlList=get(gcf,'Userdata'); FcHndl = hndlList(11); Fshndl = hndlList(12); btn1Hndl = hndlList(13); btn2Hndl = hndlList(14); Kfhndl = hndlList(18); Kphndl = hndlList(19); SNRhndl = hndlList(20); 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'); Kp = get(Kphndl,'UserData'); SNR = get(SNRhndl,'UserData'); % SNR 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 load handel Fs = get(Fshndl,'UserData'); % Sampling frequency m = y'; t = (0:length(m)-1)/Fs; 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 N = 2048; 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.'},... 'FMPMnoise Error','error','modal')) end %Fm modulated signal s = cos(2*pi*Fc*t+2*pi*Kf*int_m); sn= awgn(s, SNR, 'measured'); s_f= fft(s, N); s_f= fftshift (s_f); %Fourier Trans. of the modulated signal sn_f= fft(sn, N); sn_f= fftshift (sn_f); %Fourier Trans. of the modulated signal, noisy diff_s = (2*pi*Fc+2*pi*Kf*m).*sin(2*pi*Fc*t+2*pi*Kf*int_m); diff_sn = [0 diff(sn)]*Fs; [v, phase] = env_phas(s, 1/Fs, Fc); phi = unwrap(phase); to = diff(phi)*Fs/(2*pi*Kf); to = [0 to]; [v, phase] = env_phas(sn, 1/Fs, Fc); phi = unwrap(phase); tn = diff(phi)*Fs/(2*pi*Kf); tn = [0 tn]; else strcmp(method,'PM') n = m; m = int_m; check = find((Fc + Kp.*n)<0); %check = find((Fc + Kp.*m)<0); if isfinite(check) waitfor(msgbox({'Sorry, Fc+Kp*m(t) must be positive for all t.'... 'Some signals can be wrong.'},... 'FMPMnoise Error','error','modal')) end %Pm modulated signal s = cos(2*pi*Fc*t+2*pi*Kp*m); %PM sn= awgn(s, SNR, 'measured'); s_f= fft(s, N); s_f= fftshift (s_f); %Fourier Trans. of the fm modulated signal sn_f= fft(sn, N); sn_f= fftshift (sn_f); %Fourier Trans. of the fm modulated signal %derivative of the modulated signal diff_s = (2*pi*Fc+2*pi*Kp*n).*sin(2*pi*Fc*t+2*pi*Kp*m); diff_sn = [0 diff(sn)]*Fs; [v, phase] = env_phas(s, 1/Fs, Fc); phi = unwrap(phase); to = diff(phi)*Fs/(2*pi*Kp); to = [0 to]; [v, phase] = env_phas(sn, 1/Fs, Fc); phi = unwrap(phase); tn = diff(phi)*Fs/(2*pi*Kp); tn = [0 tn]; int_to=zeros(1, length(to)); int_tn=zeros(1, length(tn)); for i = 1:Fs-1; int_to(i+1)=int_to(i)+to(i)/(Fs-1); int_tn(i+1)=int_tn(i)+tn(i)/(Fs-1); end to = int_to; tn = int_tn; end %output in time domain noisy case if sound == 1, % original soundsc(m, Fs); elseif sound == 2, % received soundsc(sn, Fs); elseif sound == 3, % demodulated soundsc(tn, Fs); end set(gcf,'Pointer', 'arrow') 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); SNRhndl = hndlList(20); 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 SNR = get(SNRhndl,'UserData'); % SNR 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 load handel Fs = get(Fshndl,'UserData'); % Sampling frequency m = y'; t = (0:length(m)-1)/Fs; 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 N = 2048; 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.'},... 'FMPMnoise Error','error','modal')) end n = int_m; %Fm modulated signal s = cos(2*pi*Fc*t+2*pi*Kf*int_m); sn= awgn(s, SNR, 'measured'); %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 sn_f= fft(sn, N); sn_f= fftshift (sn_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); diff_sn = [0 diff(sn)]*Fs; [v, phase] = env_phas(s, 1/Fs, Fc); phi = unwrap(phase); to = diff(phi)*Fs/(2*pi*Kf); to = [0 to]; [v, phase] = env_phas(sn, 1/Fs, Fc); phi = unwrap(phase); tn = diff(phi)*Fs/(2*pi*Kf); tn = [0 tn]; else strcmp(method,'PM') n = m; m = int_m; check = find((Fc + Kp.*n)<0); %check = find((Fc + Kp.*m)<0); if isfinite(check) waitfor(msgbox({'Sorry, Fc+Kp*m(t) must be positive for all t.'... 'Some signals can be wrong.'},... 'FMPMnoise Error','error','modal')) end %Pm modulated signal s = cos(2*pi*Fc*t+2*pi*Kp*m); %PM sn= awgn(s, SNR, 'measured'); s_f= fft(s, N); s_f= fftshift (s_f); %Fourier Trans. of the fm modulated signal sn_f= fft(sn, N); sn_f= fftshift (sn_f); %Fourier Trans. of the fm modulated signal, noisy %derivative of the modulated signal diff_s = (2*pi*Fc+2*pi*Kp*n).*sin(2*pi*Fc*t+2*pi*Kp*m); diff_sn = [0 diff(sn)]*Fs; [v, phase] = env_phas(s, 1/Fs, Fc); phi = unwrap(phase); to = diff(phi)*Fs/(2*pi*Kp); to = [0 to]; [v, phase] = env_phas(sn, 1/Fs, Fc); phi = unwrap(phase); tn = diff(phi)*Fs/(2*pi*Kp); tn = [0 tn]; int_to=zeros(1, length(to)); int_tn=zeros(1, length(tn)); for i = 1:Fs-1; int_to(i+1)=int_to(i)+to(i)/(Fs-1); int_tn(i+1)=int_tn(i)+tn(i)/(Fs-1); end to = int_to; tn = int_tn; end axes(mhndl), plot(t,m), grid on ylabel('Message S') axes(mfhndl), plot(x_f,abs(m_f)), grid on ylabel('FT of Message S') axes(chndl), plot(t, s), grid on ylabel('Mod. Signal') axes(cfhndl), plot(x_f,abs(s_f)), grid on ylabel('FT of Mod. Signal') axes(shndl), plot(t,sn), grid on ylabel('Mod. Sgn. with noise') axes(sfhndl), plot(x_f,abs(sn_f)), grid on ylabel('FT of Mod. Sgn with noise') axes(rhndl), plot(t,diff_s), grid on ylabel('Derivative of Mod S') axes(fhndl), plot(t,to), grid on ylabel('Demodulated S') axes(fohndl), plot(t,diff_sn), grid on ylabel('Derivative of Mod S with noise') axes(thndl), plot(t,tn), grid on ylabel('Demodulated S with noise') xlabel('Time (Seconds)') set(gcf,'Pointer', 'arrow') return elseif strcmp(action,'info'), set(gcf,'pointer','arrow') ttlStr = get(gcf,'Name'); hlpStr= [... 'This demo lets you experiment with noisy FM and PM. ' ' ' 'Message signal and its Fourier transform are shown on the first row. ' 'On the second row modulated signal is displayed both in time domain and in ' 'frequency domain. ' ' ' 'Reception and demodulation of the modulated signal results in the following' 'graphs. On the fourth row the derivative of the modulated signal ' 'and demodulated signal are shown. ' ' ' 'Fc and Fs are the carrier and sampling frequencies, respectively, in Hertz.' 'Kf is the frequency modulation parameter. Kp is the phase modulation ' 'parameter. ' ' ' 'In each modulation scheme, a "carrier signal" (cosine) of frequency Fc is ' 'altered in some way by the message signal: ' ' FM - frequency of carrier is a function of the message signal ' ' PM - phase of carrier is a function of the message signal ' ]; myFig = gcf; helpwin(hlpStr,ttlStr); elseif strcmp(action,'closehelp'), % Restore close button help behind helpwin's back ch = get(gcf,'ch'); for i=1:length(ch), if strcmp(get(ch(i),'type'),'uicontrol'), if strcmp(lower(get(ch(i),'String')),'close'), callbackStr = get(ch(i),'callback'); k = findstr('; FMPMnoise(',callbackStr); callbackStr = callbackStr(1:k-1); set(ch(i),'callback',callbackStr) break; end end end ch = get(0,'ch'); if ~isempty(find(ch==s)), figure(s), end end