我的语音信号处理滤波去噪的GUI界面设计

function audio_player_GUI

    % 创建GUI窗口
    figure('Name', '音频播放器', 'NumberTitle', 'off', 'Position', [300, 300, 1200, 600]);

    % 创建选择音频按钮
    audio_button = uicontrol('Style', 'pushbutton', 'String', '选择音频', 'Position', [20, 460, 100, 30], 'Callback', @select_audio_callback);

    % 创建播放音频按钮
    play_button = uicontrol('Style', 'pushbutton', 'String', '播放音频', 'Position', [140, 460, 100, 30], 'Enable', 'off', 'Callback', @play_audio_callback);
    
    % 创建正弦加噪按钮
    sine_noise_button = uicontrol('Style', 'pushbutton', 'String', '正弦加噪', 'Position', [260, 460, 100, 30], 'Enable', 'off', 'Callback', @add_sine_noise_callback);

    % 创建高斯加噪按钮
    gauss_noise_button = uicontrol('Style', 'pushbutton', 'String', '高斯加噪', 'Position', [380, 460, 100, 30], 'Enable', 'off', 'Callback', @add_gauss_noise_callback);
    
    % 创建低通滤波按钮
    lowpass_button = uicontrol('Style', 'pushbutton', 'String', '低通滤波', 'Position', [20, 400, 100, 30], 'Enable', 'off', 'Callback', @lowpass_filter_callback);
    
    % 创建高通滤波按钮
    highpass_button = uicontrol('Style', 'pushbutton', 'String', '高通滤波', 'Position', [140, 400, 100, 30], 'Enable', 'off', 'Callback', @highpass_filter_callback);
    
    % 创建带通滤波按钮
    bandpass_button = uicontrol('Style', 'pushbutton', 'String', '带通滤波', 'Position', [260, 400, 100, 30], 'Enable', 'off', 'Callback', @bandpass_filter_callback);
    
    % 创建带阻滤波按钮
    bandstop_button = uicontrol('Style', 'pushbutton', 'String', '带阻滤波', 'Position', [380, 400, 100, 30], 'Enable', 'off', 'Callback', @bandstop_filter_callback);
    
    % 创建原音频的时域图和频域图
    time_axes_orig = axes('Parent', gcf, 'Position', [0.2, 0.1, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    freq_axes_orig = axes('Parent', gcf, 'Position', [0.55, 0.1, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    
    % 创建处理后的音频的时域图和频域图
    time_axes_processed = axes('Parent', gcf, 'Position', [0.2, 0.4, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    freq_axes_processed = axes('Parent', gcf, 'Position', [0.55, 0.4, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    
    % 创建去噪后的音频的时域图和频域图
    time_axes_filtered = axes('Parent', gcf, 'Position', [0.2, 0.7, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    freq_axes_filtered = axes('Parent', gcf, 'Position', [0.55, 0.7, 0.28, 0.18], 'Box', 'on', 'XGrid', 'on', 'YGrid', 'on');
    
    % 初始化全局变量
    global audio fs player
    audio = [];
    fs = [];
    player = [];

    % 选择音频文件回调函数
    function select_audio_callback(hObject, eventdata, handles)
        % 打开文件选择对话框,获取文件路径和名称
        [file_name, path_name] = uigetfile({'*.wav;*.mp3;*.flac;*.m4a'}, '选择一个音频文件');

        % 如果用户已经选择了文件,就执行以下操作
        if file_name ~= 0
            % 读取音频文件,存储在全局变量中
            [audio, fs] = audioread(fullfile(path_name, file_name));

            % 绘制原音频的时域图和频域图
            plot(time_axes_orig, audio);
            title(time_axes_orig, '原音频时域图');
            xlabel(time_axes_orig, '时间 (s)');
            ylabel(time_axes_orig, '幅度');
            plot(freq_axes_orig, log(abs(fft(audio))));
            title(freq_axes_orig, '原音频频域图');
            xlabel(freq_axes_orig, '频率 (Hz)');
            ylabel(freq_axes_orig, '幅度 (dB)');

            % 激活播放音频和添加噪声的按钮
            set(play_button, 'Enable', 'on');
            set(sine_noise_button, 'Enable', 'on');
            set(gauss_noise_button, 'Enable', 'on');
        end
    end

    % 播放音频回调函数
    function play_audio_callback(hObject, eventdata, handles)
        % 检查是否已创建音频播放器
        if isempty(player)
            player = audioplayer(audio, fs);
        end

        % 播放音频
        play(player);
    end
        % 正弦加噪回调函数
    function add_sine_noise_callback(hObject, eventdata, handles)
        % 获取用户输入
        prompt = {'请输入正弦频率 (Hz):', '请输入正弦振幅:'};
        dlg_title = '添加正弦噪声';
        num_lines = 1;
        default_ans = {'1500', '0.5'};
        answer = inputdlg(prompt, dlg_title, num_lines, default_ans);

        % 如果用户单击了取消按钮,就不执行下面的代码
        if ~isempty(answer)
            % 将用户输入转换为数字,并执行添加噪声的操作
            freq = str2double(answer{1});
            amp = str2double(answer{2});
            noisy_audio = audio + amp*sin(2*pi*freq*(0:length(audio)-1)'/fs);

            % 绘制处理后的音频的时域图和频域图
            plot(time_axes_processed, noisy_audio);
            title(time_axes_processed, '添加正弦噪声后的音频时域图');
            xlabel(time_axes_processed, '时间 (s)');
            ylabel(time_axes_processed, '幅度');
            plot(freq_axes_processed, log(abs(fft(noisy_audio))));
            title(freq_axes_processed, '添加正弦噪声后的音频频域图');
            xlabel(freq_axes_processed, '频率 (Hz)');
            ylabel(freq_axes_processed, '幅度 (dB)');

            % 创建并播放添加噪声后的音频
            player = audioplayer(noisy_audio, fs);
            play(player);
        end
    end

    % 高斯加噪回调函数
    function add_gauss_noise_callback(hObject, eventdata, handles)
        % 获取用户输入
        prompt = {'请输入噪声方差:'};
        dlg_title = '添加高斯噪声';
        num_lines = 1;
        default_ans = {'0.1'};
        answer = inputdlg(prompt, dlg_title, num_lines, default_ans);

        % 如果用户单击了取消按钮,就不执行下面的代码
        if ~isempty(answer)
            % 将用户输入转换为数字,并执行添加噪声的操作
            var = str2double(answer{1});
            noisy_audio = audio + sqrt(var)*randn(size(audio));

            % 绘制处理后的音频的时域图和频域图
            plot(time_axes_processed, noisy_audio);
            title(time_axes_processed, '添加高斯噪声后的音频时域图');
            xlabel(time_axes_processed, '时间 (s)');
            ylabel(time_axes_processed, '幅度');
            plot(freq_axes_processed, log(abs(fft(noisy_audio))));
            title(freq_axes_processed, '添加高斯噪声后的音频频域图');
            xlabel(freq_axes_processed, '频率 (Hz)');
            ylabel(freq_axes_processed, '幅度 (dB)');

            % 创建并播放添加噪声后的音频
            player = audioplayer(noisy_audio, fs);
            play(player);
        end
    end
    % 低通滤波回调函数
    function lowpass_filter_callback(hObject, eventdata, handles)
        global audio_sample_rate;
        global noisy_audio;
        global noisy_audio_player;
        
        % 获取低通滤波器的截止频率
        prompt = {'输入低通滤波器的截止频率(Hz):'};
        dlgtitle = '低通滤波器';
        dims = [1 35];
        definput = {'1000'};
        answer = inputdlg(prompt,dlgtitle,dims,definput);
        cutoff_frequency = str2double(answer{1});
        
        % 构造低通滤波器
        filter_order = 5;
        nyquist_frequency = audio_sample_rate / 2;
        normalized_cutoff_frequency = cutoff_frequency / nyquist_frequency;
        [b, a] = butter(filter_order, normalized_cutoff_frequency, 'low');
        
        % 应用滤波器
        filtered_audio_data = filter(b, a, noisy_audio);
        
        % 播放滤波后的音频
        noisy_audio_player.stop();
        noisy_audio_player = audioplayer(filtered_audio_data, audio_sample_rate);
        play(noisy_audio_player);
    end
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值