首页 文章

通过Matlab GUI读取音频文件

提问于
浏览
1

我想在matlab中创建一个有2个按钮的GUI . 1按钮(按钮1)加载所选文件,第二个按钮(按钮2)执行代码 . 这就是我到目前为止所拥有的

% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, ventdata, handles)
    % hObject    handle to pushbutton1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    [filename pathname] = uigetfile({'*.wav'}, 'Select File');
    fullpathname = strcat (pathname, filename);
    audio = wavread(fullpathname);


    % --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

    % get a section of vowel
    [x,fs]=wavread('audio',[24120 25930]);    
    ms20=fs/50; % minimum speech Fx at 50Hz

    % plot waveform
    t=(0:length(x)-1)/fs; % times of sampling instants
    subplot(2,1,1);
    plot(t,x);
    legend('Waveform');
    xlabel('Time (s)');
    ylabel('Amplitude');

错误在以下行中

[x,fs]=wavread('audio',[24120 25930]);

我该怎么写呢?

此外,在绘制如何使绘图出现在GUI上时?

4 回答

  • 1

    wavread 将文件名作为第一个参数 . 由于 audio 不是当前路径中的文件(或者可能不是您想要的路径),因此您不能将其作为第一个参数 .

    但变量 audio 保存信号本身,因此您不需要再访问文件本身 . 然后,同时初始化 fs

    [audio,fs] = wavread(fullpathname);
    

    然后,如果您需要获取信号的一部分,只需获取一部分:

    x = audio(24120 25930);
    

    对于绘图,在GUI中添加轴并使用这些轴的句柄作为第一个参数调用 plot (Matlab的文档中充满了示例:)) .

  • 0

    [data,fs] = wavread(filename);

    x =数据(24120:25930);

    %来绘制数据

    情节(handles.axes1,T,X);

    %%假设您没有更改轴的句柄名称

  • 0

    我试过你的建议,我写的是这样的:

    % --- Executes on button press in pushbutton1.
        function pushbutton1_Callback(hObject, eventdata, handles)
        % hObject    handle to pushbutton1 (see GCBO)
        % eventdata  reserved - to be defined in a future version of MATLAB
        % handles    structure with handles and user data (see GUIDATA)
        [filename pathname] = uigetfile({'*.wav'}, 'Select File');
        fullpathname = strcat (pathname, filename);
        [data,fs] = wavread(filename);
    
    
    
        % --- Executes on button press in pushbutton2.
        function pushbutton2_Callback(hObject, eventdata, handles)
        % hObject    handle to pushbutton2 (see GCBO)
        % eventdata  reserved - to be defined in a future version of MATLAB
        % handles    structure with handles and user data (see GUIDATA)
        %MODEL
        % get a section of vowel
        x = data(24120:25930);
        ms20=fs/50; % minimum speech Fx at 50Hz
        %
        % plot waveform
        t=(0:length(x)-1)/fs; % times of sampling instants
        subplot(2,1,1);
        plot(t,x);
        legend('Waveform');
        xlabel('Time (s)');
        ylabel('Amplitude');
        plot(handles.axes1,t,x);
        %
        % calculate autocorrelation
        r=xcorr(x,ms20,'coeff');
        %
        % plot autocorrelation
        d=(-ms20:ms20)/fs; % times of delays
        subplot(2,1,2);
        plot(d,r);
        legend('Autocorrelation');
        xlabel('Delay (s)');
        ylabel('Correlation coeff.');
        plot(handles.axes2,d,r);
        ms2=fs/500 % maximum speech Fx at 500Hz
        ms20=fs/50 % minimum speech Fx at 50Hz
        % just look at region corresponding to positive delays
        r=r(ms20+1:2*ms20+1)
        [rmax,tx]=max(r(ms2:ms20))
        fprintf('rmax=%g Fx=%gHz\n',rmax,fs/(ms2+tx-1));
    

    但我得到以下错误:

    ?? Undefined function or method 'data' for input arguments of type 'double'.
    
    Error in ==> untitled>pushbutton2_Callback at 94
    x = data(24120:25930);
    
    Error in ==> gui_mainfcn at 96
            feval(varargin{:});
    
    Error in ==> untitled at 42
        gui_mainfcn(gui_State, varargin{:});
    
    Error in ==> @(hObject,eventdata)untitled('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
    
    
    ??? Error while evaluating uicontrol Callback
    
  • 0

    你的问题是变量ure给wavread读取,而不是 [data,fs] = wavread(filename); use:[data,fs] = wavread(fullpathname) . 它对我有用 .

相关问题