You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
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?
7 views (last 30 days)
Show older comments
[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 Comments
SUCHITHRA K S
on 23 Oct 2018
Error using downsample Too many output arguments.
Error in project (line 8) [y,fs2]=downsample(x,20,441);
Answers (1)
madhan ravi
on 23 Oct 2018
Edited: madhan ravi
on 23 Oct 2018
Should be downsample but you wrote it as downsqample
17 Comments
madhan ravi
on 23 Oct 2018
downsample only should have one output but you have two that’s causing the error
SUCHITHRA K S
on 23 Oct 2018
but i have to plot that downsampled audio then how i will plot if i remove fs2 ?
SUCHITHRA K S
on 23 Oct 2018
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);
>>
madhan ravi
on 23 Oct 2018
I don’t have any experience in audio sampling but what I am sure that downsample() should have only one output
Walter Roberson
on 23 Oct 2018
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
SUCHITHRA K S
on 23 Oct 2018
Edited: Walter Roberson
on 23 Oct 2018
[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);
Walter Roberson
on 23 Oct 2018
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
SUCHITHRA K S
on 24 Oct 2018
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?
Walter Roberson
on 24 Oct 2018
Your audio contains multiple channels, which is fine for resample(). However spectrogram can only process one channel.
SUCHITHRA K S
on 24 Oct 2018
then what is the comment for plotting the spectrogram of resampled audio
Walter Roberson
on 24 Oct 2018
Edited: Walter Roberson
on 27 Jan 2021
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
madhan ravi
on 27 Oct 2018
Edited: madhan ravi
on 27 Oct 2018
Thank you sir Walter . @Suchithra you can appreciate the persons time by accepting the answer and also it indicates the question is solved
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)