I get the error at line 49 and 62 "Too many input arguments. Too many input arguments" and another error "Error using findpeaks>parse_inputs Expected Y to be nonempty."
    조회 수: 10 (최근 30일)
  
       이전 댓글 표시
    
I get this error at line 49 and 62:
"Too many input arguments.
Too many input arguments" 
"Error using findpeaks>parse_inputs
Expected Y to be nonempty.
Error in findpeaks (line 135)
  = parse_inputs(isInMATLAB,Yin,varargin{:});"
classdef app_one < matlab.apps.AppBase
    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        InstrumentLabel  matlab.ui.control.Label
        EditField3       matlab.ui.control.EditField
        ResetButton      matlab.ui.control.Button
        AnalyzeButton    matlab.ui.control.Button
        RecordButton     matlab.ui.control.Button
        UIAxes2          matlab.ui.control.UIAxes
        UIAxes           matlab.ui.control.UIAxes
    end
    properties (Access = private)
        fs % Description
        nob % Description
        noc % Description
        audio % Description
        y % Description
        t % Description
        nfft % Description
        f % Description
        ymax % Description
        f1 % Description
        f2 % Description
        xymax % Description
        xmax % Description
        val % Description
    end
    % Callbacks that handle component events
    methods (Access = private)
        % Button pushed function: RecordButton
        function RecordButtonPushed(app, event)
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            app.RecordButton.BackgroundColor='g';
            app.fs=48000;
            app.nob=16;
            app.noc=2;
            app.audio=audioDeviceReader('SampleRate', app.fs, 'NumChannels', app.noc);
            app.y=record(app.audio, app.fs * 5)';
            app.RecordButton.BackgroundColor='w';
        end
        % Button pushed function: AnalyzeButton
        function AnalyzeButtonPushed(app, event)
            app.nfft=2^nextpow2(length(app.y));
            app.f=linspace(0,app.fs,app.nfft);
            app.y=abs(fft(app.y,app.nfft));
            % Find the highest peaks in the frequency spectrum
            [pks,locs] = findpeaks(app.y(2:app.nfft/2),'MinPeakHeight',0.1*max(app.y(2:app.nfft/2)),'MinPeakDistance',50);
            locs = locs + 1;
            % Match peak frequencies with known frequency ranges of different instruments
            instrument_names = {'Open Hi-Hat', 'Close Hi-Hat', 'Big Crash', 'Small Crash', 'Splash', 'Ride', 'Ride Bell', 'Floor Tom', 'High Tom', 'Low Tom', 'Bass Drum', 'Snare Drum'};
            instrument_freq_ranges = {[7633,7787], [11702,11938], [5038,5140], [3992,4072], [6020,6142], [3524,3596], [3693,3767], [75,77], [166,170], [109,113], [30,80], [400,3000]};
            detected_instruments = cell(1,length(locs)); % Preallocate cell array with fixed size
            for i = 1:length(locs)
                detected_instruments{i} = {};
                for j = 1:length(instrument_freq_ranges)
                    if locs(i) > instrument_freq_ranges{j}(1) && locs(i) < instrument_freq_ranges{j}(2)
                        detected_instruments{i} = instrument_names{j};
                    end
                end
            end
            % Display detected instruments
            if ~isempty(detected_instruments)
                app.EditField3.Value = strjoin(detected_instruments, ', ');
            else
                app.EditField3.Value = 'Unknown instrument';
            end
            % Plot frequency spectrum with peak frequencies marked
            plot(app.UIAxes2,app.f(2:app.nfft/2),app.y(2:app.nfft/2),'r');
            hold(app.UIAxes2, 'on');
            plot(app.UIAxes2,app.f(locs),pks,'*b');
            hold(app.UIAxes2, 'off');
        end
        % Button pushed function: ResetButton
        function ResetButtonPushed(app, event)
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            clear sound;
            close all;
        end
    end
    % Component initialization
    methods (Access = private)
        % Create UIFigure and components
        function createComponents(app)
            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Time Domain')
            xlabel(app.UIAxes, 'Time')
            ylabel(app.UIAxes, 'Amplitude')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [1 153 313 328];
            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'Frequency Domain')
            xlabel(app.UIAxes2, 'Frequency')
            ylabel(app.UIAxes2, 'Amplitude')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.Position = [329 153 300 328];
            % Create RecordButton
            app.RecordButton = uibutton(app.UIFigure, 'push');
            app.RecordButton.ButtonPushedFcn = createCallbackFcn(app, @RecordButtonPushed, true);
            app.RecordButton.Position = [69 98 100 23];
            app.RecordButton.Text = 'Record';
            % Create AnalyzeButton
            app.AnalyzeButton = uibutton(app.UIFigure, 'push');
            app.AnalyzeButton.ButtonPushedFcn = createCallbackFcn(app, @AnalyzeButtonPushed, true);
            app.AnalyzeButton.Position = [259 97 100 23];
            app.AnalyzeButton.Text = 'Analyze';
            % Create ResetButton
            app.ResetButton = uibutton(app.UIFigure, 'push');
            app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
            app.ResetButton.Position = [438 97 100 23];
            app.ResetButton.Text = 'Reset';
            % Create EditField3
            app.EditField3 = uieditfield(app.UIFigure, 'text');
            app.EditField3.Position = [282 38 100 22];
            % Create InstrumentLabel
            app.InstrumentLabel = uilabel(app.UIFigure);
            app.InstrumentLabel.Position = [301 59 62 22];
            app.InstrumentLabel.Text = 'Instrument';
            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end
    % App creation and deletion
    methods (Access = public)
        % Construct app
        function app = app_one
            % Create UIFigure and components
            createComponents(app)
            % Register the app with App Designer
            registerApp(app, app.UIFigure)
            if nargout == 0
                clear app
            end
        end
        % Code that executes before app deletion
        function delete(app)
            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
댓글 수: 0
답변 (1개)
  Rahul
    
 2023년 3월 1일
        
      편집: Rahul
    
 2023년 3월 1일
  
      The first error is received because it is using "record"  (to be removed in further MATLAB version) which is generally used to record data and event information to file for the serial port object. Also, it does not expect any output arguments as per the syntax given in the documentation page. Another function "record" which is used to record audio to "audiorecorder" object (and not "audioDeviceReader" object mentioned in your code). This also does not expect any output argument. The limitation to the "record" function is that it does not pause the execution until the recording is complete unlike "recordblocking". Moreover, the second argument to the "record" or "recordblocking" function is the recording time in seconds which you have given fs * 5 (this is very high). Instead, I have added "timelimit" property in private access properties in both the approaches. This asks user to input for how many seconds to record the sound.
I have mentioned two approaches where in Approch 1, I have used "audioDeviceReader" object and in appoach 2, I have used "audiorecorder" object. Necessary comments are added in the code for better understanding. 
Approach 1:
In this approach, I have used "audioDeviceReader" object. The sound data is then saved in a WAV file format and later accessed using "audioread" function.
classdef app_one < matlab.apps.AppBase
    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        InstrumentLabel  matlab.ui.control.Label
        EditField3       matlab.ui.control.EditField
        ResetButton      matlab.ui.control.Button
        AnalyzeButton    matlab.ui.control.Button
        RecordButton     matlab.ui.control.Button
        UIAxes2          matlab.ui.control.UIAxes
        UIAxes           matlab.ui.control.UIAxes
    end
    properties (Access = private)
        fs % Description
        nob % Description
        noc % Description
        audio % Description
        y % Description
        t % Description
        nfft % Description
        f % Description
        ymax % Description
        f1 % Description
        f2 % Description
        xymax % Description
        xmax % Description
        val % Description
        timelimit % newly added
    end
    % Callbacks that handle component events
    methods (Access = private)
        % Button pushed function: RecordButton
        function RecordButtonPushed(app, event)
            app.RecordButton.BackgroundColor='g';
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            app.fs=48000;
            app.nob=16;
            app.noc=2;
            app.audio=audioDeviceReader('SampleRate', app.fs, 'NumChannels', app.noc);
%             app.y=record(app.audio, app.fs * 5)';
            %%%%%%%%%%%%%%%% newly added %%%%%%%%%%%%%%%%%%%%%%
            % the following set of commands records the sound for a
            % a given timelimit using attached microphone.
            setup(app.audio)
            fileWriter = dsp.AudioFileWriter('mySpeech.wav','FileFormat','WAV');
            prompt = {'Enter a value of time to record the audio (in seconds)'};
            dlgtitle = 'Recording time limit';
            dims = [1 40];
            definput = {'10'};
            opts.Interpreter = 'tex';
            app.timelimit = inputdlg(prompt,dlgtitle,dims,definput,opts);
            app.timelimit = str2double(app.timelimit{1});
            tic
            while toc < app.timelimit
                app.y = app.audio();
                fileWriter(app.y);
            end
            msgbox('Recording complete.')
            release(app.audio)
            release(fileWriter)
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            app.RecordButton.BackgroundColor='w';
        end
        % Button pushed function: AnalyzeButton
        function AnalyzeButtonPushed(app, event)
            [app.y, ~] = audioread('mySpeech.wav'); % newly added
            % this reads the saved WAV file
            app.nfft=2^nextpow2(length(app.y));
            app.f=linspace(0,app.fs,app.nfft);
            app.y=abs(fft(app.y,app.nfft));
            % Find the highest peaks in the frequency spectrum
            [pks,locs] = findpeaks(app.y(2:app.nfft/2),'MinPeakHeight',0.1*max(app.y(2:app.nfft/2)),'MinPeakDistance',50);
            locs = locs + 1;
            % Match peak frequencies with known frequency ranges of different instruments
            instrument_names = {'Open Hi-Hat', 'Close Hi-Hat', 'Big Crash', 'Small Crash', 'Splash', 'Ride', 'Ride Bell', 'Floor Tom', 'High Tom', 'Low Tom', 'Bass Drum', 'Snare Drum'};
            instrument_freq_ranges = {[7633,7787], [11702,11938], [5038,5140], [3992,4072], [6020,6142], [3524,3596], [3693,3767], [75,77], [166,170], [109,113], [30,80], [400,3000]};
            detected_instruments = cell(1,length(locs)); % Preallocate cell array with fixed size
            for i = 1:length(locs)
                detected_instruments{i} = {};
                for j = 1:length(instrument_freq_ranges)
                    if locs(i) > instrument_freq_ranges{j}(1) && locs(i) < instrument_freq_ranges{j}(2)
                        detected_instruments{i} = instrument_names{j};
                    else
                        detected_instruments{i} = 'not listed'; % newly added to avoid any error in strjoin
                    end
                end
            end
            % Display detected instruments
            if ~isempty(detected_instruments)
                app.EditField3.Value = strjoin(detected_instruments, ', ');
            else
                app.EditField3.Value = 'Unknown instrument';
            end
            %%%%%%%%%%%%%%%% newly added %%%%%%%%%%%%%%%%%%%%%%
            % Plot time domain signal for each channel
            timeWindow = linspace(0,app.timelimit, length(app.y));
            plot(app.UIAxes, timeWindow, app.y(:, 1), 'b')
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % Plot frequency spectrum with peak frequencies marked
            plot(app.UIAxes2, app.f(2:app.nfft/2), app.y(2:app.nfft/2),'r');
            hold(app.UIAxes2, 'on');
            plot(app.UIAxes2,app.f(locs),pks,'*b');
            hold(app.UIAxes2, 'off');
        end
        % Button pushed function: ResetButton
        function ResetButtonPushed(app, event)
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            clear sound;
            close all;
        end
    end
    % Component initialization
    methods (Access = private)
        % Create UIFigure and components
        function createComponents(app)
            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Time Domain')
            xlabel(app.UIAxes, 'Time')
            ylabel(app.UIAxes, 'Amplitude')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [1 153 313 328];
            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'Frequency Domain')
            xlabel(app.UIAxes2, 'Frequency')
            ylabel(app.UIAxes2, 'Amplitude')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.Position = [329 153 300 328];
            % Create RecordButton
            app.RecordButton = uibutton(app.UIFigure, 'push');
            app.RecordButton.ButtonPushedFcn = createCallbackFcn(app, @RecordButtonPushed, true);
            app.RecordButton.Position = [69 98 100 23];
            app.RecordButton.Text = 'Record';
            % Create AnalyzeButton
            app.AnalyzeButton = uibutton(app.UIFigure, 'push');
            app.AnalyzeButton.ButtonPushedFcn = createCallbackFcn(app, @AnalyzeButtonPushed, true);
            app.AnalyzeButton.Position = [259 97 100 23];
            app.AnalyzeButton.Text = 'Analyze';
            % Create ResetButton
            app.ResetButton = uibutton(app.UIFigure, 'push');
            app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
            app.ResetButton.Position = [438 97 100 23];
            app.ResetButton.Text = 'Reset';
            % Create EditField3
            app.EditField3 = uieditfield(app.UIFigure, 'text');
            app.EditField3.Position = [282 38 100 22];
            % Create InstrumentLabel
            app.InstrumentLabel = uilabel(app.UIFigure);
            app.InstrumentLabel.Position = [301 59 62 22];
            app.InstrumentLabel.Text = 'Instrument';
            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end
    % App creation and deletion
    methods (Access = public)
        % Construct app
        function app = app_one
            % Create UIFigure and components
            createComponents(app)
            % Register the app with App Designer
            registerApp(app, app.UIFigure)
            if nargout == 0
                clear app
            end
        end
        % Code that executes before app deletion
        function delete(app)
            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
Approach 2:
In this approch, the "audiorecorder" function is used which creates an object to record audio data from an input device such as a microphone for processing in MATLAB. The audiorecorder object contains properties that enable additional flexibility during recording. For example, you can pause, resume, or define callbacks using the audiorecorder object functions. Then, "getaudiodata"  helps to extract the audio signal as a numeric array of different data types from the "audiorecorder" object. This approach alleviates saving the recorded signal to a WAV file as mentioned in Approach 1. 
classdef app_one < matlab.apps.AppBase
    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        InstrumentLabel  matlab.ui.control.Label
        EditField3       matlab.ui.control.EditField
        ResetButton      matlab.ui.control.Button
        AnalyzeButton    matlab.ui.control.Button
        RecordButton     matlab.ui.control.Button
        UIAxes2          matlab.ui.control.UIAxes
        UIAxes           matlab.ui.control.UIAxes
    end
    properties (Access = private)
        fs % Description
        nob % Description
        noc % Description
        audio % Description
        y % Description
        t % Description
        nfft % Description
        f % Description
        ymax % Description
        f1 % Description
        f2 % Description
        xymax % Description
        xmax % Description
        val % Description
        timelimit % newly added
    end
    % Callbacks that handle component events
    methods (Access = private)
        % Button pushed function: RecordButton
        function RecordButtonPushed(app, event)
            app.RecordButton.BackgroundColor='g';
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            app.fs=48000;
            app.nob=16;
            app.noc=2;
            %%%%%%%%%%%%%%%% newly added %%%%%%%%%%%%%%%%%%%%%%
            % the following set of commands records the sound for a
            % a given timelimit using attached microphone.
%             setup(app.audio)
            prompt = {'Enter a value of time to record the audio (in seconds)'};
            dlgtitle = 'Recording time limit';
            dims = [1 40];
            definput = {'10'};
            opts.Interpreter = 'tex';
            app.timelimit = inputdlg(prompt,dlgtitle,dims,definput,opts);
            app.timelimit = str2double(app.timelimit{1});
            app.audio = audiorecorder(app.fs, app.nob, app.noc);
            app.audio.StartFcn = 'msgbox(''Start speaking.'')';
            app.audio.StopFcn = 'msgbox(''End of recording.'')';
            recordblocking(app.audio, app.timelimit);
            app.y = getaudiodata(app.audio);
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            app.RecordButton.BackgroundColor='w';
        end
        % Button pushed function: AnalyzeButton
        function AnalyzeButtonPushed(app, event)
            app.nfft=2^nextpow2(length(app.y));
            app.f=linspace(0,app.fs,app.nfft);
            app.y=abs(fft(app.y,app.nfft));
            % Find the highest peaks in the frequency spectrum
            [pks,locs] = findpeaks(app.y(2:app.nfft/2),'MinPeakHeight',0.1*max(app.y(2:app.nfft/2)),'MinPeakDistance',50);
            locs = locs + 1;
            % Match peak frequencies with known frequency ranges of different instruments
            instrument_names = {'Open Hi-Hat', 'Close Hi-Hat', 'Big Crash', 'Small Crash', 'Splash', 'Ride', 'Ride Bell', 'Floor Tom', 'High Tom', 'Low Tom', 'Bass Drum', 'Snare Drum'};
            instrument_freq_ranges = {[7633,7787], [11702,11938], [5038,5140], [3992,4072], [6020,6142], [3524,3596], [3693,3767], [75,77], [166,170], [109,113], [30,80], [400,3000]};
            detected_instruments = cell(1,length(locs)); % Preallocate cell array with fixed size
            for i = 1:length(locs)
                detected_instruments{i} = {};
                for j = 1:length(instrument_freq_ranges)
                    if locs(i) > instrument_freq_ranges{j}(1) && locs(i) < instrument_freq_ranges{j}(2)
                        detected_instruments{i} = instrument_names{j};
                    else
                        detected_instruments{i} = 'not listed'; % newly added to avoid any error in strjoin
                    end
                end
            end
            % Display detected instruments
            if ~isempty(detected_instruments)
                app.EditField3.Value = strjoin(detected_instruments, ', ');
            else
                app.EditField3.Value = 'Unknown instrument';
            end
            %%%%%%%%%%%%%%%% newly added %%%%%%%%%%%%%%%%%%%%%%
            % Plot time domain signal for each channel
            timeWindow = linspace(0,app.timelimit, length(app.y));
            plot(app.UIAxes, timeWindow, app.y(:, 1), 'b')
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % Plot frequency spectrum with peak frequencies marked
            plot(app.UIAxes2, app.f(2:app.nfft/2), app.y(2:app.nfft/2),'r');
            hold(app.UIAxes2, 'on');
            plot(app.UIAxes2,app.f(locs),pks,'*b');
            hold(app.UIAxes2, 'off');
        end
        % Button pushed function: ResetButton
        function ResetButtonPushed(app, event)
            app.EditField3.Value= '-';
            plot(app.UIAxes2,0,0);
            plot(app.UIAxes,0,0);
            clear sound;
            close all;
        end
    end
    % Component initialization
    methods (Access = private)
        % Create UIFigure and components
        function createComponents(app)
            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Time Domain')
            xlabel(app.UIAxes, 'Time')
            ylabel(app.UIAxes, 'Amplitude')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [1 153 313 328];
            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'Frequency Domain')
            xlabel(app.UIAxes2, 'Frequency')
            ylabel(app.UIAxes2, 'Amplitude')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.Position = [329 153 300 328];
            % Create RecordButton
            app.RecordButton = uibutton(app.UIFigure, 'push');
            app.RecordButton.ButtonPushedFcn = createCallbackFcn(app, @RecordButtonPushed, true);
            app.RecordButton.Position = [69 98 100 23];
            app.RecordButton.Text = 'Record';
            % Create AnalyzeButton
            app.AnalyzeButton = uibutton(app.UIFigure, 'push');
            app.AnalyzeButton.ButtonPushedFcn = createCallbackFcn(app, @AnalyzeButtonPushed, true);
            app.AnalyzeButton.Position = [259 97 100 23];
            app.AnalyzeButton.Text = 'Analyze';
            % Create ResetButton
            app.ResetButton = uibutton(app.UIFigure, 'push');
            app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
            app.ResetButton.Position = [438 97 100 23];
            app.ResetButton.Text = 'Reset';
            % Create EditField3
            app.EditField3 = uieditfield(app.UIFigure, 'text');
            app.EditField3.Position = [282 38 100 22];
            % Create InstrumentLabel
            app.InstrumentLabel = uilabel(app.UIFigure);
            app.InstrumentLabel.Position = [301 59 62 22];
            app.InstrumentLabel.Text = 'Instrument';
            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end
    % App creation and deletion
    methods (Access = public)
        % Construct app
        function app = app_one
            % Create UIFigure and components
            createComponents(app)
            % Register the app with App Designer
            registerApp(app, app.UIFigure)
            if nargout == 0
                clear app
            end
        end
        % Code that executes before app deletion
        function delete(app)
            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
If this solution worrks for you, please accept the answer.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!