How to check Number of channels of a sound file and convert stereo file in mono in MATLAB

41 ビュー (過去 30 日間)
I want to check whether the .wav file is stereo or mono.If it is stereo, convert in mono using MATLAB.How to proceed

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 6 月 22 日
編集済み: Geoff Hayes 2017 年 6 月 22 日
Hemangi - try using audioread as
myWavFile = 'some.wav';
[y,Fs] = audioread(myWavFile);
From audioread y output argument, y will be an m-by-n matrix, where m is the number of audio samples read and n is the number of audio channels in the file. So if n is greater than one, then you have multiple channels.

その他の回答 (2 件)

Jan
Jan 2017 年 6 月 22 日
And a conversion to mono:
yMono = sum(y, 2) / size(y, 2);

JoshuaT
JoshuaT 2019 年 6 月 26 日
A couple of points:
1) Taking the average of the channels is not usually recommended for mixing down to stereo. Instead, it is common to sum the two channels but normalize them according to the absolute value of the maximum peak in either channel (https://en.wikipedia.org/wiki/Audio_normalization).
2) Hemanji appears to be asking to conditionally mix the stereo down. That is, the sound file may already be mono in which case we would get an error if we try to mix it down.
An example script that accomplishes all of this might look like this:
[x,fs] = audioread('myWavFile.wav');
[m, n] = size(x); %gives dimensions of array where n is the number of stereo channels
if n == 2
y = x(:, 1) + x(:, 2); %sum(y, 2) also accomplishes this
peakAmp = max(abs(y));
y = y/peakAmp;
% check the L/R channels for orig. peak Amplitudes
peakL = max(abs(x(:, 1)));
peakR = max(abs(x(:, 2)));
maxPeak = max([peakL peakR]);
%apply x's original peak amplitude to the normalized mono mixdown
y = y*maxPeak;
else
y = x; %it is stereo so we will return it as is (e.g., for additional processing)
end
  1 件のコメント
vaibhav gaur
vaibhav gaur 2022 年 10 月 29 日
hello joshua, I came across the audioread function in my assignment and cant understand what does the channel mean here. can you please elaborate.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePurple についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by