⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 moddemo.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
📖 第 1 页 / 共 2 页
字号:
    messageHndl = hndlList(12);

    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
    vv = round(vv*10)/10;
    if Fc>vv/2
        waitfor(msgbox({'Sorry, the sampling frequency Fs must be at least'...
                  'twice the carrier frequency Fc.'},...
                 'Moddemo Error','error','modal'))
        vv = v;
    end
    if vv<30
        waitfor(msgbox(['Sorry, the sampling frequency Fs' ...
                        ' must be at least 30.'],...
                 'Moddemo Error','error','modal'))
        vv = v;
    end
    set(gco,'Userdata',vv,'String',num2str(vv))
    if vv == v
        set(fig,'Pointer','arrow');
        return
    end

    waveform = get(waveHndl,'value');
    if waveform==1,   % speech waveform - have to use resample
        modHndl = hndlList(2);
        w = get(waveHndl,'userdata');
        [p,q] = rat(vv/7418,.0001);
       makeinvislist = [modHndl get(modHndl,'children')' get(modHndl,'ylabel')];
        set(messageHndl,'string',...
          sprintf('Resampling waveform at %g/%g\ntimes original rate ...',p,q));
        set(makeinvislist,'visible','off')
        set(messageHndl,'visible','on')
        drawnow
        load mtlb
        yy = resample(mtlb,p,q);
        tt = (0:length(yy)-1)'/vv;
        set(waveHndl,'userdata',[tt yy]);
        set(messageHndl,'visible','off')
        set(makeinvislist,'visible','on')
        moddemo('modulate')
    else
        Fs = get(FsHndl,'UserData'); % Sampling frequency
        Fc = get(FcHndl,'UserData'); % Carrier frequency
        moddemo('changewave',Fc,Fs)
    end
    return

elseif strcmp(action,'changewave')
% create new waveform and stuff it into userdata of popup,
% then call moddemo('modulate')
    set(gcf,'Pointer','watch');
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    origHndl = hndlList(1);
    modHndl = hndlList(2);
    demodHndl = hndlList(3);
    waveHndl = hndlList(4);
    FcHndl = hndlList(5);
    FsHndl = hndlList(6);

    waveform = get(waveHndl,'value');
    if waveform==2,
      if nargin<2
          Fc = 400;
          Fs = 2000;
      else
          Fc = s;
          Fs = ss;
      end
      t = (0:1/Fs:2)';
      y = sin(2*pi*t);
    elseif waveform==3,
      if nargin<2
          Fc = 400;
          Fs = 2000;
      else
          Fc = s;
          Fs = ss;
      end
      t = (0:1/Fs:2)';
      y = square(2*pi*t);
    elseif waveform==4,
      if nargin<2
          Fc = 400;
          Fs = 2000;
      else
          Fc = s;
          Fs = ss;
      end
      t = (0:1/Fs:2)';
      y = sawtooth(2*pi*t,.5);
    elseif waveform==1,
      load mtlb
      Fc = 2000;
      Fs = 7418;
      y = mtlb;
      t = (0:length(y)-1)'/Fs;
    end
    set(FcHndl,'string',num2str(Fc),'userdata',Fc);
    set(FsHndl,'string',num2str(Fs),'userdata',Fs);
    
    set(waveHndl,'userdata',[t y])
    moddemo('modulate')

    return

elseif strcmp(action,'playsound'),
    set(gcf,'Pointer','watch');

    hndlList=get(gcf,'Userdata');
    modHndl = hndlList(2);
    waveHndl = hndlList(4);
    FcHndl = hndlList(5);
    FsHndl = hndlList(6);
    btn1Hndl = hndlList(8);
    messageHndl = hndlList(12);

    Fc = get(FcHndl,'UserData'); % carrier frequency
    Fs = get(FsHndl,'UserData'); % Sampling frequency
    method = get(btn1Hndl,'Userdata');
    y = get(waveHndl,'userdata');
    y = y(:,2);

    if s == 1,  % original
        sstr = 'original';
    elseif s == 2,  % modulated
        sstr = 'modulated';
    elseif s == 3,  % demodulated
        sstr = 'demodulated';
    end
    c = computer;
    if (strcmp(c,'SUN4') | strcmp(c,'HP700') | strcmp(c,'SOL2'))
        play_Fs = 8192;
    else
        play_Fs = Fs;
    end
    str = sprintf('Playing %s waveform \nat %g Hz ...',sstr,play_Fs);
    set(messageHndl,'string',str)
    makeinvislist = [modHndl get(modHndl,'children')' get(modHndl,'ylabel')];
    set(makeinvislist,'visible','off')
    set(messageHndl,'visible','on')
    drawnow

    if s >= 2   % need to compute modulated or demodulated wave
        if strcmp(method,'fm')
            kf = (Fc/Fs)*pi/2; 
            y1 = modulate(y,Fc,Fs,method,kf);
            if s == 3
                y2 = (1/kf)*demod(y1,Fc,Fs,method);
            end
        elseif strcmp(method,'pm')
            kp = pi/2/max(max(abs(y)));
            y1 = modulate(y,Fc,Fs,method,kp);
            if s == 3
                y2 = (1/kp)*demod(y1,Fc,Fs,method);
            end
        else
            y1 = modulate(y,Fc,Fs,method);
            if s == 3
                y2 = demod(y1,Fc,Fs,method);
            end
        end
    end
    if s == 1
        soundStr = 'soundsc(y,play_Fs)';
    elseif s == 2
        soundStr = 'soundsc(y1,play_Fs)';
    elseif s == 3
        soundStr = 'soundsc(y2,play_Fs)';
    end
    eval(soundStr,'')  % catch errors in case no sound capabilities

    set(messageHndl,'visible','off')
    set(makeinvislist,'visible','on')

    set(gcf,'Pointer','arrow');
    return

elseif strcmp(action,'radio'),
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    for i=8:11,
      set(hndlList(i),'value',0) % Disable all the buttons
    end
    set(hndlList(s+7),'value',1) % Enable selected button
    set(hndlList(8),'Userdata',ss) % Remember selected button
    moddemo('modulate')
    return

elseif strcmp(action,'modulate'), % modulate, demodulate, and update display
    set(gcf,'Pointer','watch');
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    origHndl = hndlList(1);
    modHndl = hndlList(2);
    demodHndl = hndlList(3);
    waveHndl = hndlList(4);
    FcHndl = hndlList(5);
    FsHndl = hndlList(6);
    dispHndl = hndlList(7);
    btn1Hndl = hndlList(8);
    btn2Hndl = hndlList(9);
    btn3Hndl = hndlList(10);
    btn4Hndl = hndlList(11);

    set(gcf,'nextplot','add')

    edgecolor = get(gca,'colororder'); edgecolor = edgecolor(1,:);
    
    Fs = get(FsHndl,'UserData'); % Sampling frequency
    Fc = get(FcHndl,'UserData'); % Carrier frequency

    w = get(waveHndl,'userdata');
    t = w(:,1);  y = w(:,2);
    
    method = get(btn1Hndl,'Userdata');

    if strcmp(method,'fm')
        % kf = (Fc/Fs)*2*pi/max(abs(y));
        kf = (Fc/Fs)*pi/2; 
        y1 = modulate(y,Fc,Fs,method,kf);
        y2 = (1/kf)*demod(y1,Fc,Fs,method);
    elseif strcmp(method,'pm')
        kp = pi/2/max(max(abs(y)));
        y1 = modulate(y,Fc,Fs,method,kp);
        y2 = (1/kp)*demod(y1,Fc,Fs,method);
    else
        y1 = modulate(y,Fc,Fs,method);
        y2 = demod(y1,Fc,Fs,method);
    end
    if get(dispHndl,'value')==1,   % time waveform
      axes(origHndl), plot(t,y), grid on
      ylabel('Original')
      axes(modHndl), strips(y1,(length(y1)-1)/8/Fs,Fs), grid on
      ylabel('Modulated')
      axes(demodHndl), plot(t,y2), grid on
      ylabel('Demodulated')
      xlabel('Time (Seconds)')
    elseif get(dispHndl,'value')==2,   % psd
      [Y,f] = psd(y,1024,Fs,500,0,'none');
      [Y1,f] = psd(y1,1024,Fs,500,0,'none');
      [Y2,f] = psd(y2,1024,Fs,500,0,'none');
      Y = 10*log10(Y);
      Y1 = 10*log10(Y1);
      Y2 = 10*log10(Y2);
      axes(origHndl), plot(f,Y), grid on, ylabel('Original')
      axes(modHndl), plot(f,Y1), grid on, ylabel('Modulated')
      axes(demodHndl), plot(f,Y2), grid on, ylabel('Demodulated')
      xlabel('Frequency (Hz)')
      set(hndlList(1:3),'ylim', ...
       [max(max(min([Y Y1 Y2])),-120) max(max([Y Y1 Y2]))],'xlim',[0 Fs/2])
    else  % specgram
      k = 150;
      wind = kaiser(128,5);
      nwind = length(wind);
      noverlap = max(fix((length(y) - k*nwind) / (1-k)),0);
      [b,f,t] = specgram(y,128,Fs,wind,noverlap);
      [b1,f,t] = specgram(y1,256,Fs,wind,noverlap);
      [b2,f,t] = specgram(y2,128,Fs,wind,noverlap);
      Y1 = 10*log10(abs(b1+eps));
      clims = [min(min(Y1)) max(max(Y1))];
      axes(modHndl), imagesc(t,f,Y1), axis xy, colormap(jet)
      ylabel('Modulated')
      axes(origHndl), imagesc(t,f,10*log10(abs(b+eps)),clims) 
      axis xy, colormap(jet)
      ylabel('Original')
      axes(demodHndl), imagesc(t,f,10*log10(abs(b2+eps)),clims)
      axis xy, colormap(jet)
      ylabel('Demodulated')
      xlabel('Time (Seconds)')
    end

    set(gcf,'Pointer', 'arrow')
    return

elseif strcmp(action,'info'),
    set(gcf,'pointer','arrow')
	ttlStr = get(gcf,'Name');
	hlpStr1= [...
             ' This demo lets you experiment with 4 different   '
             ' modulation schemes. They are:                    '
             '                                                  '
             '      AM - amplitude modulation                   '
             '      AMSSB - amplitude modulation, single side-  '
             '               band                               '
             '      FM - frequency modulation                   '
             '      PM - phase modulation                       '
             '                                                  '
             ' The demo uses MODULATE and DEMOD in the          ' 
             ' Signal Processing Toolbox to implement           '
             ' these schemes.                                   '
             '                                                  '
             ' The message signal is displayed in the top left  '
             ' plot.  Its modulated version is displayed in the '
             ' middle left plot.  The demodulated version of    '
             ' the modulated signal (the "reconstructed"        '
             ' waveform) is displayed in the bottom left plot.  ' ];

	hlpStr2 = [...
             ' The popup menus on the upper right of the        '
             ' figure control:                                  '
             '                                                  '
             '     - how to display the signals (upper popup)   '
             '        CHOICES:                                  '
             '        time - time domain waveform               '
             '        psd - power spectral density (frequency   '
             '              domain)                             '
             '        specgram - spectrogram (time AND          '
             '              frequency domain) [NOT AVAILABLE    '
             '                           IN STUDENT EDITION]    '
             '     - which message signal to use (lower popup)  '
             '        CHOICES:                                  '
             '        speech - digitized speech waveform,       '
             '              originally sampled at 7418 Hz.      '
             '        sine, square, triangle - 2 seconds worth  '
             '              of 1 Hertz basic waveforms.         '];

	hlpStr3 = [...
             ' Fc and Fs are the carrier and sampling           '
             ' frequencies, respectively, in Hertz.             '
             '                                                  '
             ' In each modulation scheme, a "carrier signal"    '
             ' (cosine) of frequency Fc is altered in some      '
             ' way by the message signal:                       '
             '     AM - amplitude of carrier is message signal  '
             '     AMSSB - two carriers (one cosine, one sine)  '
             '       are modulated by the message signal and    '
             '       its Hilbert transform respectively, and    '
             '       added together                             '
             '     FM - instantaneous frequency of carrier is   '
             '       message signal                             '
             '     PM - instantaneous phase of carrier is       '
             '       message signal                             '
             '                                                  '
             ' The "Play ..." buttons let you listen to the     '
             ' signals on most computers.                       '];

    myFig = gcf;
    helpfun(ttlStr,hlpStr1,hlpStr2,hlpStr3);

elseif strcmp(action,'closehelp'),
    % Restore close button help behind helpfun'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('; moddemo(',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 % Make sure figure exists

end    % if strcmp(action, ...lose all

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -