how to downsample a audio having sampling rate 44100 Hz to 2000Hz in matlab? This is the program i used but it showing error. how to rectify it?

[x, fs1] = audioread('audio8.wav');
ts1=1/fs1;
N1=length(x);
Tmax1=(N1-1)*ts1;
t1=(0:ts1:Tmax1);
figure;
plot(t1,x),xlabel('Time'),title('Original audio');
[y,fs2]=downsqample(x,20,441);
ts2=1/fs2;
N2=length(y);
Tmax2=(N2-1)*ts2;
t2=(0:ts2:Tmax2);
figure;
plot(t2,y),xlabel('Time'),title('downsampled audio');

2 commentaires

What’s the error message paste everything in red here
Error using downsample Too many output arguments.
Error in project (line 8) [y,fs2]=downsample(x,20,441);

Connectez-vous pour commenter.

Réponses (1)

madhan ravi
madhan ravi le 23 Oct 2018
Modifié(e) : madhan ravi le 23 Oct 2018
Should be downsample but you wrote it as downsqample

17 commentaires

downsample only should have one output but you have two that’s causing the error
[y,fs2]=downsample(x,20,441);
Should be
y=downsample(x,20,441);
but i have to plot that downsampled audio then how i will plot if i remove fs2 ?
now it showing a error like this
Error using updownsample>parseUpDnSample (line 55) Offset must be from 0 to N-1.
Error in updownsample (line 17) phase = parseUpDnSample(str,N,varargin{:});
Error in downsample (line 33) y = updownsample(x,N,'Down',varargin{:});
Error in project (line 8) y=downsample(x,20,441);
>>
I don’t have any experience in audio sampling but what I am sure that downsample() should have only one output
For those inputs you probably want resample not downsample.
resample does permit two outputs but the second is not fs2. Your fs2 should be 20/441*fs1
[x, fs1] = audioread('audio2.wav');
sound(x,fs1);
ts1=1/fs1;
N1=length(x);
Tmax1=(N1-1)*ts1;
t1=(0:ts1:Tmax1);
figure;
plot(t1,x),xlabel('Time'),title('Original audio');
fs2 = (20/441)*fs1;
y=downsample(x,20,441);
sound(y,fs2);
ts2=1/fs2;
N2=length(y);
Tmax2=(N2-1)*ts2;
t2=(0:ts2:Tmax2);
figure;
plot(t2,y),xlabel('Time'),title('downsampled audio');
i tried the code but the error is like this,
Error using updownsample>parseUpDnSample (line 55)
Offset must be from 0 to N-1.
Error in updownsample (line 17)
phase = parseUpDnSample(str,N,varargin{:});
Error in downsample (line 33)
y = updownsample(x,N,'Down',varargin{:});
Error in project (line 10)
y=downsample(x,20,441);
Read the documentation for downsample:
downsample(X,N) downsamples input signal X by keeping every
N-th sample starting with the first. If X is a matrix, the
downsampling is done along the columns of X.
downsample(X,N,PHASE) specifies an optional sample offset.
PHASE must be an integer in the range [0, N-1].
I believe that what you want to do instead is to rate convert 44100 Hz to 22000 Hz, which is a ratio of 22 out of 441 . To do that you would use resample not downsample
if true
% code
end
[x, fs1] = audioread('audio2.wav');
%sound(x,fs1);
ts1=1/fs1;
N1=length(x);
Tmax1=(N1-1)*ts1;
t1=(0:ts1:Tmax1);
figure;
plot(t1,x),xlabel('Time'),title('Original audio');
fs2 = (20/441)*fs1;
y=resample(x,2000,44100);
%sound(y,fs2);
ts2=1/fs2;
N2=length(y);
Tmax2=(N2-1)*ts2;
t2=(0:ts2:Tmax2);
figure;
plot(t2,y),xlabel('Time'),title('resampled audio');
spectrogram(y, 256, [], 25, 2000, 'yaxis');
when i plot the spectrogram of the resampled signal it showing the error like this ">> resam Error using spectrogram>chkinput (line 252) X must be a vector (either row or column).
Error in spectrogram (line 166) chkinput(x);
Error in resam (line 18) spectrogram(y, 256, [], 25, 2000, 'yaxis');
>> " how to rectify it?
Your audio contains multiple channels, which is fine for resample(). However spectrogram can only process one channel.
then what is the comment for plotting the spectrogram of resampled audio
nchan = size(y,2);
for chan = 1 : nchan
subplot(1, nchan, chan)
spectrogram(y(:,chan), 256, [], 25, 2000, 'yaxis');
title( sprintf('spectrogram of channel #%d', chan) );
end
Thank you sir Walter . @Suchithra you can appreciate the persons time by accepting the answer and also it indicates the question is solved
Sir Walter indeed, as usual. @SUCHITHRA K S, I strongly 2nd @madhan ravi's comment

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by