通过Matlab GUI读取音频文件

时间:2014-01-15 04:28:29

标签: matlab matlab-guide

我想在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 个答案:

答案 0 :(得分:1)

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

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

[audio,fs] = wavread(fullpathname);

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

x = audio(24120 25930);

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

答案 1 :(得分:0)

[data,fs] = wavread(filename);

x =数据(24120:25930);

%来绘制数据

积(handles.axes1,T,X);

%%假设您未将句柄名称更改为轴

答案 2 :(得分: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

答案 3 :(得分:0)

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