i have code for ploting the spectrogram of a resampled audio but the program showing error .

11 views (last 30 days)
this is the code i tried ;
[audio, fs1] = audioread('audio2.wav');
sound(audio,fs1);
ts1=1/fs1;
N1=length(audio);
Tmax1=(N1-1)*ts1;
t1=(0:ts1:Tmax1);
fsu1=fs1/(N1-1);
f1=(-fs1/2:fsu1:fs1/2);
figure;
plot(t1,x),xlabel('Time'),title('Original audio');
fs2 = (20/441)*fs1;
audiore=resample(x,2000,44100);
sound(audiore,fs2);
ts2=1/fs2;
N2=length(audiore);
Tmax2=(N2-1)*ts2;
t2=(0:ts2:Tmax2);
fsu2=fs2/(N2-1);
f2=(-fs2/2:fsu2:fs2/2);
figure;
plot(t2,audiore),xlabel('Time'),title('resampled audio');
function outputSpectrogram(input)
[audioresam, fs] = audioread('audiore');
audioMono = (audioresam(:, 1) + audioresam(:, 2)) / 2;
spectrogram(audioresam, 256, [], 25, 2000, 'yaxis');
title('spectrogram of resampled audio')
end
it showing the error like this "">> resam Error: File: resam.m Line: 1 Column: 1 Function definitions are not permitted in this context.""
how to rectify it??

Answers (1)

Riya
Riya on 10 Feb 2025
Hi Suchithra,
I understand that you are encountering an error while executing the code.
In MATLAB version R2016a and earlier, function definitions inside scripts are not supported. To solve this issue, you can move the function definition of “outputSpectrogram” function in a separate file named “outputSpectrogram.m” and call the function inside the script using the command:
outputSpectrogram('audiore.wav');
In MATLAB version R2016b and later, function definitions inside scripts are supported and you would not encounter this error.
To get more information about functions in scripts, please refer to the following document:
Thanks!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!