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

📄 component_window_launcher.m

📁 此代码仿真了一个语音输入输出系统
💻 M
📖 第 1 页 / 共 4 页
字号:
            % Create DAC window.
            dac = figure('position', dac_dims,...
            'menubar', 'none',...
            'numbertitle', 'off',...
            'resize', 'off',...
            'WindowButtonDownFcn', 'global spread_input_player; stop(spread_input_player);',... % stop spread playback
            'name', '8. Digital to Analog Conversion');
        
            % Add the DAC text.
            dac_text = uicontrol(dac,...
                'style', 'text',...
                'fontsize', dac_text_fontsize,...
                'foregroundcolor', [.5 0 0],...
                'backgroundcolor', [.8 .8 .8],... % same gray as GUI background
                'horizontalalignment', 'center',...
                'string', dac_string,...
                'units', 'normalized',...
                'position', dac_text_dims);
            
        case 'despread_pb'
            % Create de-Spread window.
            despread = figure('position', despread_dims,...
                'menubar', 'none',...
                'numbertitle', 'off',...
                'resize', 'off',...
                'WindowButtonDownFcn', 'global spread_input_player; stop(spread_input_player);',... % stop spread playback
                'name', '9. de-Spread');
            
            % Create axes for the received spread and de-spread signals and plot.
            axes('position', despread_axes_1, 'box', 'on');
            plot(voice_data_spread_received, 'magenta');
            title('Spread Received Voice Signal');
            ylabel('Perceived Pressure');
            xlabel('Sample');
            axes('position', despread_axes_2, 'box', 'on');
            plot(voice_data_received, 'magenta');
            title('Received Voice Signal (amplitude clipped)');
            ylabel('Perceived Pressure');
            xlabel('Sample');
            
            % Add de-Spread text.
            despread_text = uicontrol(despread,...
                'style', 'text',...
                'fontsize', despread_text_fontsize,...
                'foregroundcolor', [.5 0 0],...
                'backgroundcolor', [.8 .8 .8],... % same gray as GUI background
                'horizontalalignment', 'center',...
                'string', despread_string,...
                'units', 'normalized',...
                'position', despread_text_dims);
            
        case 'speakers_pb'
            % Create speakers window.
            speakers = figure('position', speakers_dims,...
                'menubar', 'none',...
                'numbertitle', 'off',...
                'resize', 'off',...
                'WindowButtonDownFcn', 'global spread_input_player; stop(spread_input_player);',... % stop spread playback
                'name', '10. Received Voice Playback');
            
            % Create input and received signal axes.
            axes('position', speakers_axes_1_dims, 'box', 'on');
            plot(voice_data, 'blue');
            title('Input Voice Signal');
            ylabel('Perceived Pressure');
            xlabel('Sample');
            axes('position', speakers_axes_2_dims, 'box', 'on');
            plot(voice_data_received, 'magenta');
            title('Received Voice Signal');
            ylabel('Perceived Pressure');
            xlabel('Sample');
        
            % Instructions.
            spkrs_instructions = uicontrol(speakers,...
                'style', 'text',...
                'fontsize', spkrs_instructions_fontsize,...
                'foregroundcolor', [.5 0 0],...
                'backgroundcolor', [.8 .8 .8],... % same gray as GUI background
                'horizontalalignment', 'center',...
                'string', spkrs_string,...
                'units', 'normalized',...
                'position', spkrs_instructions_dims);
            
            % Outro message (larger text).
            spkrs_outro = uicontrol(speakers,...
                'style', 'text',...
                'fontsize', spkrs_outro_fontsize,...
                'foregroundcolor', [.5 0 0],...
                'backgroundcolor', [.8 .8 .8],... % same gray as GUI background
                'horizontalalignment', 'center',...
                'string', spkrs_outro_string,...
                'units', 'normalized',...
                'position', spkrs_outro_dims);           
        
            % Pushbuttons.
            input_hear = uicontrol(speakers,...
                    'style', 'pushbutton',...
                    'string', 'Hear Input',...
                    'foregroundcolor', 'black',...
                    'fontsize', sprks_pb_fontsize,...
                    'units', 'normalized',...
                    'position', input_hear_dims,...
                    'callback', @play_input_spkrs_cb);
            received_hear = uicontrol(speakers,...
                    'style', 'pushbutton',...
                    'string', 'Hear Received Signal',...
                    'foregroundcolor', 'black',...
                    'fontsize', sprks_pb_fontsize,...
                    'units', 'normalized',...
                    'position', receieved_hear_dims,...
                    'callback', @play_received_cb);
            sprks_save = uicontrol(speakers,...
                    'style', 'pushbutton',...
                    'string', 'Save',...
                    'foregroundcolor', 'black',...
                    'fontsize', sprks_pb_fontsize,...
                    'units', 'normalized',...
                    'position', sprks_save_dims,...
                    'callback', @spkrs_save_cb);
end

%----------------------------WINDOW LAUNCH CONTROL---------------------------%



%------------------------------CALLBACK FUNCTIONS----------------------------%

% OPEN pushbutton callback
    function [] = mic_open_cb(obj, eventdata)
    
    % Make the voice data and Hear and Continue buttons readily available.
    global voice_data mic_hear mic_continue;
    
    % Create an open file dialog box.
    [filename, pathname] = uigetfile('*.wav',...
        'Select An Input Signal');
    
    % If the user didn't pick a file (i.e. they clicked cancel), return.
    if filename == 0
        return
    else    
        % Get the voice data.
        voice_data = wavread(filename, 8000);
    
        % Clip the data and keep only the left channel.
        voice_data = voice_data(1:8000, 1);
    
        % Plot the waveform.
        axis([0 8000 -1 1]);
        plot(voice_data, 'blue');
        title('Input Signal');
        ylabel('Perceived Pressure');
        xlabel('Sample');
        
        % Enable the Hear button.
        set(mic_hear, 'enable', 'on');
        
        % Enable the Continue button.
        set(mic_continue, 'enable', 'on');
        
        % Create the audioplayer object.
        input_player = audioplayer(voice_data, 8000);
    end
    
% CONTINUE pushbutton callback
    function [] = mic_continue_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global voice_data
    
    % Ensure that the user has specified an input before closing the window and
    % moving on to the spread component.
    if size(voice_data) == [0 0]
        msgbox('Please specify a voice input signal',...
            'No Input Signal Specified', 'warn');
    else
        close;
        spread('full_spread', 2);
    end
    
% SPREAD FACTOR FIELD callback
    function [] = spread_factor_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global spread_factor_field spread_axes voice_data_spread
    global walsh walsh_field
    
    % Valid spreading factors.
    valid_inputs = [1 2 4 8 16 32 64 128 256];
    
    % Check to see if input is valid.  The easiest way to do this is to use
    % the valid_inputs vector and the MATLAB ismember function (rather than
    % trying complicated math).  These checks are not 100% specific (ie the
    % user could enter 299.97 and get the "256 is the largest supported"
    % message), but they cover all bases.
    if ismember(str2num(get(spread_factor_field, 'string')), valid_inputs) % if input was valid, do next checks
        if str2num(get(spread_factor_field, 'string')) == 1 % tell user about a spreading factor of 1
            msgbox(['A spreading factor of 1 is "valid," but 1 is the resulting Walsh Code.  ',...
                    'Because of this, no spreading would take place (and, therefore, the same ',...
                    'waveform would appear in both plots).'],...
                'Spreading Factor of 1', 'help');
        else
            spread('user_spread', str2num(get(spread_factor_field, 'string')));
            axes(spread_axes); % make the spread plot axes the current axes
            plot(voice_data_spread, 'blue');
            title('Spread Input Signal');
            ylabel('Perceived Pressure');
            xlabel('Sample');
            set(walsh_field, 'string', num2str(walsh')); % place the Walsh Code in the Walsh Code field
        end
    elseif str2num(get(spread_factor_field, 'string')) > 256 % 3SA supports spreading factors up to and including 256 only
        msgbox('3SA does not support valid spreading factors larger than 256.',...
                'Spreading Factor is Too Large', 'help');
    else % otherwise the user didn't even enter an integer
         msgbox(['Walsh Code spreading factors must be integer values resulting from 2^n, where n is an integer ',...
                 'greater than or equal to 1 (a spreading factor of 1 is valid, but results in no spreading)'],...
             'Invalid Spreading Factor', 'warn');       
    end

% PLAY INPUT SIGNAL pushbutton callback
    function [] = play_input_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global input_player spread_input_player
    
    % Stop the spread signal's playback.
    stop(spread_input_player);
        
    % Play the input signal.
    play(input_player);
    
% PLAY SPREAD SIGNAL pushbutton callback
    function [] = play_spread_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global spread_input_player input_player
    
    % Stop the input signal's playback.
    stop(input_player);
        
    % Play the spread signal.
    play(spread_input_player);

% STOP SPREAD SIGNAL pushbutton callback
    function [] = stop_spread_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global spread_input_player
    
    % Silence the spread input signal.
    stop(spread_input_player);
    
% SAVE RECEIVED SIGNAL pushbutton callback
    function [] = spkrs_save_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global voice_data_received
    
    % Create a save file dialog box.
    [filename, pathname] = uiputfile('received_voice.wav',...
        'Save Received Signal');
    
    % Save 3SA's current directory path.
    current_directory = cd;
    
    % If the user did not click Cancel, save their file.
    if pathname ~= 0
        cd(pathname);
        wavwrite(voice_data_received, filename);
    end
    
    % Return to 3SA directory.
    cd(current_directory);
    
% PLAY INPUT SIGNAL (speakers) pushbutton callback
    function [] = play_input_spkrs_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global input_player recieved_input_player
        
    % Play the input signal.
    play(input_player);
    
% PLAY RECEIVED SIGNAL pushbutton callback
    function [] = play_received_cb(obj, eventdata)
    
    % Globalize simulation data that this function must access.
    global received_input_player input_player voice_data_received
    
    % Create audioplayer object.
    received_input_player = audioplayer(voice_data_received, 8000);
    
    % Stop the input signal's playback.
    stop(input_player);
        
    % Play the spread signal.
    play(received_input_player);

%------------------------------CALLBACK FUNCTIONS----------------------------%

⌨️ 快捷键说明

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