Domanda

I want to make a GUI in matlab that has 2 buttons. 1 Button (pushbutton1) loads the selected file and the second button (pushbutton2) executes a code. This is what i have so far

    % --- 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');


The mistake is in the following line

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

How should I write it?

Also when plotting how do I make the plot appear on the GUI ?

È stato utile?

Soluzione

wavread takes a filename as first argument. As audio is not a file in your current path (or maybe not the one you want), you cannot put this as first argument.

But the variable audio holds the signal itself, so you don't need to access to the file itself anylonger. Then, initialize fs at the same time:

[audio,fs] = wavread(fullpathname);

Then, if you need to pick up a part of your signal, just get a slice of it:

x = audio(24120 25930);

For plotting, add axes in your GUI and call plot with the handle of these axes as first parameters (doc of Matlab is full of examples of that :) ).

Altri suggerimenti

[data,fs] = wavread(filename);

x = data(24120:25930);

% to plot data

plot(handles.axes1,t,x);

%%assuming you didn't change the name of handle to axis

I tried your suggestions and I wrote it like this :

    % --- 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));

But I get the following erros:

?? 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

ur problem was in the variable ure giving for wavread to read, instead of [data,fs] = wavread(filename); use:[data,fs] = wavread(fullpathname). It worked for me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top