I am getting an error like function definitions are not permitted in this context in matlab R2014b
1 回表示 (過去 30 日間)
古いコメントを表示
function [Soundfunction] = Soundfunction( file1, file2 );
clf % Clears the graphic screen.
close all;% Close all figures (except those of imtool.)
clc;% Clear the command window.
fontSize = 20;
fontSize1 = 14;
% Reads in the sound files, into a big array called y1 and y2.
%y = wavread( file );
[y1, fs1]= audioread( file1 );
[y2, fs2]= audioread( file2 );
% Normalize y1; that is, scale all values to its maximum. Note how simple it
is
% to do this in MatLab.
yn1 = y1/max(abs(y1));
yn2 = y1/max(abs(y1));
sound1 = y1;
sound2 = y2;
%% Do not modify here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% time of sound
N = length(sound1); % number of points to analyze
ls = size (sound1); % find the length of the data per second
nbits = 2^(length(sound1));
t1 = (0:1:length(sound1)-1)/fs1; %% time1
fx = fs1*(0:N/2-1)/N; %Prepare freq data for plot
T1 = 1/fs1 % period between each sample
N2 = length(sound2);
ls2 = size (sound2); % find the length of the data per second
t2 = (0:1:length(sound2)-1)/fs2;
fx2 = fs2*(0:N2/2-1)/N2; %Prepare freq data for plot
T2 = 1/fs2 % period between each sample
%%%%% cut signal for comparing
% cut signal to same length
voice1 = y1(fs1*1 : fs1*9);
voice2 = y2(fs2*1 : fs2*9);
%% find new length
N2x = length(voice2);
N1x = length(voice1);
% find new time
t1x = (0:1:length(voice1)-1)/fs1;
t2x = (0:1:length(voice2)-1)/fs2;
%% find new frequency
f2x = fs2*(0:N2x/2-1)/N2x;
f1x = fs1*(0:N1x/2-1)/N1x;
%% fft of cut signal
NFFT1x = 2 ^ nextpow2(N1x);
Y1x = fft(voice1, NFFT1x)/ N1x;
f1xx = (fs1/ 2 * linspace(0, 1, NFFT1x / 2+1))'; % Vector containing
frequencies in Hz
STFFT1x = ( 2 * abs(Y1x(1: NFFT1x / 2+1))); % Vector containing corresponding
amplitudes
NFFT2x = 2 ^ nextpow2(N2x);
Y2x = fft(voice2, NFFT2x) / N2x;
f2xx = (fs2 / 2 * linspace(0, 1, NFFT2x / 2+1))'; % Vector containing
frequencies in Hz
STFFT2x = ( 2 * abs(Y2x(1: NFFT2x / 2+1))); % Vector containing corresponding
amplitudes
%% plot for the cut signal
%% plot for the cut signal
figure; subplot (3,2,1); plot(t1x, voice1);
str1=sprintf('Plot Sound1 with sampling rate = %d Hz and number sample = %d',
fs1, N1x);
title(str1);
xlabel('time (sec)'); ylabel('relative signal strength'); grid on;
subplot (3,2,2); plot(t2x, voice2);
str2=sprintf('Plot Sound2 with sampling rate = %d Hz and number sample = %d',
fs2, N2x);
title(str2); xlabel('time (sec)','Fontsize', fontSize); ylabel('relative
signal strength','Fontsize', fontSize); grid on;
%% fft of cut signal
subplot (3,2,3); plot(f1xx, STFFT1x); title ('Single-sided Power spectrum for
Sound1 with same length','Fontsize', fontSize1);
ylabel ('Magnitude [dB]','Fontsize', fontSize); xlabel ('frequency
[Hz]','Fontsize', fontSize); grid on;
subplot (3,2,4); plot(f2xx, STFFT2x); title ('Single-sided Power spectrum for
Sound2 with same length','Fontsize', fontSize1);
ylabel ('Magnitude [dB]','Fontsize', fontSize); xlabel ('frequency
[Hz]','Fontsize', fontSize); grid on;
%% corlation
[C1, lag1] = xcorr(abs((fft(voice1))),abs((fft(voice2)) ));
figure, plot(lag1/fs1,C1);
ylabel('Amplitude'); grid on
title('Cross-correlation between Sound1 and sound2 files')
%% calculate mean
% Calculate the MSE
D= voice1 - voice2;
MSE=mean(D.^2);
%msgbox(strcat('MSE value is= ',mat2str(),' MSE'));
msgbox(strcat('MSE value is= ',mat2str(MSE)));
if MSE ==0;
msgbox('no error. Signal are the same');
disp('Signal are the same');
else
disp('Signal are not the same');
msgbox('Test error. Please check your set up');
end
0 件のコメント
回答 (1 件)
madhan ravi
2019 年 2 月 14 日
P.s - I am not going to look into your code. I am just answering your main question.
https://www.mathworks.com/help/matlab/ref/function.html#description - read the second bulleted point , what does it say ? You should save the function in a separate file . The name of the function file should be the same name of the function. It’s worthwhile to read the documentation.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Audio and Video Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!