20-2 ���ɪ�Ū��

¦b¤@¯ë¹q¸£¥­¥x¤W¡A±`¨£­µ°TÀɮתºªþÀɦW¦³ .wav¡B.au¡B.mp3¡B.aifµ¥µ¥¡A§Ú­Ì³£¥i¥H¨Ï¥Î MATLAB ¨Óª½±µÅª¨ú³o´XºØÀɮסA©Ò¥Îªº«ü¥O¬Oaudioread¡C¨Ò¦p¡A­Y­nŪ¨ú¥»³¹½d¨Òµ{¦¡¥Ø¿ý¤ºªºÀÉ®× welcome.wav¡Bµe¥X­µ°Tªºªi§Î¨Ã¼½©ñ¥X¦¹­µ°T¡A¥i¨Ï¥Î¤U¦Cµ{¦¡¡G

Example 1: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioRead01.m[y, fs]=audioread('welcome.wav'); sound(y, fs); % ¼½©ñ¦¹­µ°T time=(1:length(y))/fs; % ®É¶¡¶bªº¦V¶q plot(time, y); % µe¥X®É¶¡¶b¤Wªºªi§Î

¦b¤W¨Ò¤¤¡Afs ¬O¨ú¼ËÀW²v¡]sample rate¡^¡A¨ä­È¬° 11025¡A¥Nªí·íªì¦b¿ý»s³o­Ó­µ°TÀɮ׮ɡA¨C¬íÄÁ·|°O¿ý¤U11025­ÓÁn­µ°T¸¹ªº¨ú¼Ë­È¡C¦Ó y ¬OÁn­µ°T¸¹ªº¦V¶q¡A¨Ï¥Î sound(y, fs) ¥i¼½©ñ¥X¦¹­µ°T¡A©Ò¥H§A·|Å¥¨ì§ÚªºÁn­µ¡uÅwªï¥úÁ{¡v¡Ctime «h¬O¹ïÀ³¦b¦b®É¶¡¶bªº¦V¶q¡A¦]¦¹±N y ¹ï time °µ¹Ï¡A´N±o¨ì¦b®É¶¡¶b¤W­±ªº­µ°Tªi§Î¡C

Hint
¦b¤W­z½d¨Ò¤¤¡A­µ°T²Ä¤@ÂI©Ò¹ïÀ³ªº®É¶¡¬O1/fs¡A¦ý§Ú­Ì¤]¥i¥H§ï¦¨¹ïÀ³®É¶¡¬O0¡A¦¹®É¹ïÀ³ªº®É¶¡¦V¶qÀ³¸Ó¬° time=(0:length(y)-1)/fs¡C

¨Æ¹ê¤W¦b¿ý»s­µ°TÀɮ׮ɡA¨C¤@­Ó¨ú¼ËÂI¤j³¡¤À³£¬O¥Ñ8©Î16­Ó¦ì¤¸¡]bits¡^©Ò¥Nªí¡A­Y­nª¾¹D welcome.wav ªº¨ú¼ËÂI¬O¥Ñ¦h¤Ö­Ó¦ì¤¸¨Óªí¥Ü¡A¥i¨Ï¥Î audioinfo(¡¥welcome.wav¡¦) ¨Ó¦^¶Ç¦UºØ¬ÛÃö¸ê°T¡A½d¨Ò¦p¤U¡G

Example 2: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioInfo01.mfileName='welcome.wav'; info=audioinfo(fileName); fprintf('ÀɮצWºÙ = %s\n', info.Filename); fprintf('À£ÁY¤è¦¡ = %s\n', info.CompressionMethod); fprintf('³q¹D­Ó¼Æ = %g ­Ó\n', info.NumChannels); fprintf('¨ú¼ËÀW²v = %g Hz\n', info.SampleRate); fprintf('¨ú¼ËÂIÁ`­Ó¼Æ = %g ­Ó\n', info.TotalSamples); fprintf('­µ°Tªø«× = %g ¬í\n', info.Duration); fprintf('¨ú¼ËÂI¸ÑªR«× = %g ¦ì¤¸/¨ú¼ËÂI\n', info.BitsPerSample); ÀɮצWºÙ = D:\users\jang\books\matlabProgramming4beginner\example\20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ\welcome.wav À£ÁY¤è¦¡ = Uncompressed ³q¹D­Ó¼Æ = 1 ­Ó ¨ú¼ËÀW²v = 11025 Hz ¨ú¼ËÂIÁ`­Ó¼Æ = 16001 ­Ó ­µ°Tªø«× = 1.45134 ¬í ¨ú¼ËÂI¸ÑªR«× = 8 ¦ì¤¸/¨ú¼ËÂI

Hint
¹ï©ó¤£¦PÃþ§Oªº­µÀÉ¡Aaudioinfo ©Ò¦^¶ÇªºÄæ¦ì¥i¯à¦³©Ò¤£¦P¡AŪªÌ¥i¥H¬O¬Ý¬Ý¥t¤@­Ó½d¨Ò audioInfo03.m¡A¨Ó¬Ý¬Ý¹ï©ó mp3 Àɮתº¦^¶Çµ²ªG¡C

ŪªÌ¥i¯à·|¦n©_¡A¬°¤°»ò¦b welcome.wav ­µ°TÀɮפ¤¡A¨C¤@­Ó¨ú¼ËÂI¥Î 8 ­Ó¦ì¤¸¨Óªí¥Ü¡A¦ý¬O±q­µ°Tªi§Î¨Ó¬Ý¡A¨C¤@ÂIªº­È³£¤¶©ó ¡V1 ©M 1 ¤§¶¡¡A³o¤¤¶¡¬O¦p¦óÂà´«ªº¡H­º¥ý­nª¾¹D wav Àɮתº 8 ¦ì¤¸¬O¥H unsigned integer¡]¤£±a²Å¸¹ªº¾ã¼Æ¡A§Y¥¿¾ã¼Æ¡^ªº¤è¦¡¨ÓÀx¦s¡A¦]¦¹©Ò¯àªí¥Üªº¼Æ­È¬O¤¶©ó 0 ©M 255 (2^8-1) ¤§¶¡¡AMATLAB ¦A±N¦¹­È³]©w¦ÜÅÜ¼Æ y ®É¡A·|¦Û°Ê±N¨ä¼Æ­È½Õ¾ã¦Ü¤¶©ó ¡V1 ©M 1 ¤§¶¡¡A¦]¦¹­Y­n±N MATLAB Ū¥X¤§¼Æ­ÈÂà¦^­ì¥ý 8 ¦ì¤¸©Òªí¥Ü¤§¼Æ­È¡A¥u­n±NÅÜ¼Æ y ­¼¥H 128¡A¦A¥[¤W 128¡A´N¥i¥H±o¨ì­ì¥ýªº¾ã¼Æ­È¡A¨Ò¦p¡G

Example 3: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioRead03.mfileName='welcome.wav'; [y, fs]=audioread(fileName); info=audioinfo(fileName); nbits=info.BitsPerSample; y0=y*(2^nbits/2)+(2^nbits/2); % y0 ¬O­ì¥ýÀx¦s¦b­µ°TÀɮפ¤ªº­È difference=sum(abs(y0-round(y0))) difference = 0

¦b¤W¨Ò¤¤¡AÅÜ¼Æ difference ªº­È¬O¹s¡A¥Nªí y0 ªº­È§¹¥þ¬O¾ã¼Æ¡C¦¹¥~¡A¬°¤F¼W¥[µ{¦¡½Xªº³q¥Î©Ê¡A§Ú­Ì¥Î 2^nbits/2¡A¦Ó¤£ª½±µ¨Ï¥Î128¡C

MATLAB ¹ï©ó­µÀɸê®Æªº¥¿³W¤Æ¤è¦¡¡A¥i¥H¦Cªí¦p¤U¡G

­µ°TÀɮפº³¡Àx¦s¤è¦¡ ¼Æ­È½d³ò MATLAB¥¿³W¤Æ¤è¦¡
Unsigned 8-bit integer (uint8) y $\in$ [0, $2^8-1$] (y-$2^7$)/$2^7$
Signed 16-bit integer (int16) y $\in$ [$-2^{15}$, $2^{15}-1$] y/$2^{15}$
Signed 32-bit integer (int32) y $\in$ [$-2^{31}$, $2^{31}-1$] y/$2^{31}$

audioread ¤]¥i¥HŪ¨úÂùÁn¹D©Î¥ßÅéÁn¡]Stereo¡^ªº­µ°TÀɮסA¦¹®É¶Ç¦^ªºÅܼƬ°¨ã¦³¨âª½¦æªº°}¦C¡A¨C¤@ª½¦æ¥Nªí¤@­ÓÁn¹Dªº­µ°T¡A¨Ò¦p¡G

Example 4: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioRead04.mfileName='flanger.wav'; [y, fs]=audioread(fileName); % Ū¨ú­µ°TÀÉ sound(y, fs); % ¼½©ñ­µ°T left=y(:,1); % ¥ªÁn¹D­µ°T right=y(:,2); % ¥kÁn¹D­µ°T subplot(2,1,1), plot((1:length(left))/fs, left); subplot(2,1,2), plot((1:length(right))/fs, right);

¦¹½d¨Ò·|Ū¨úÂùÁn¹Dªº­µ°TÀÉ flanger.wav¡A¼½©ñ¦¹ÂùÁn¹Dªº­µ°T¡A¨Ãµe¥X¨â­ÓÁn¹Dªº­µ°Tªi§Î¡C¥Ñ©ó¥ª¥kÁn¹Dªº­µ¶q¡]§Yªi§Îªº¾_´T¡^¬Û¤¬®øªø¡A¦]¦¹¼½©ñ¥X¨Óªº®ÄªG¡A·Pı¦n¹³­µ·½¦b¥ª¥kÁn¹D¤§¶¡´å²¾¡C¡]¤p°ÝÃD¡Gµ¹§A¤@­Ó³æÁn¹DªºÁn­µ°T¸¹¡A§A¦p¦ó¨Ï¥Î MATLAB ¹ï¦¹­µ°T¶i¦æ³B²z¡A³Ì«á²£¥Í­ì­µ°T¦bÂùÁn¹D¤§¶¡´å²¾ªº®ÄªG¡A¦p¦P¦¹½d¨Ò¤@¯ë¡H¡^

¦pªG­µ°TÀɮ׫ܤj¡AµLªk¤@¦¸Åª¤J°O¾ÐÅé¡A§Ú­Ì¤]¥i¥H¨Ï¥Î audioread ¨ÓŪ¥X­µ°TÀɪº¨ä¤¤¤@³¡¥÷¡A¨Ò¦p¡G

Example 5: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioRead05.m[y,fs]=audioread('welcome.wav', [4001 5000]); % Ū¨ú­µ°TÀɲÄ4001¦Ü5000ÂI plot(y)

³o¤@¬qªi§Î¥Nªí¡uÅwªï¥úÁ{¡v¤¤ªº¡uªï¡v­è¶}©lµo­µªºªi§Î¡C¥Ñ¤W¹Ï¥i¬Ý¥X¡AÁnªiªº¥~«¬¬O«ÜÃþ¦ü¤@­Ó¶g´Áªi¡A¨ä¾_´T¤j¤p´N¥Nªí­µ¶q¤j¤p¡A¦Ó¨ä¶g´Áªº­Ë¼Æ´N¬O¡u°ò¥»ÀW²v¡v¡C¥H¤W­z¹Ï§Î¦Ó¨¥¡A1000­Ó¨ú¼ËÂI¤j·§¥]§t¤F10­Ó¶g´Á¡A©Ò¥H¨C­Ó¶g´Á¤j¬ù¥]§t 100ÂI¡A¹ïÀ³ªº®É¶¡¬O 100/fs = 100/11025 = 0.0091¬í = 9.1 ms¡A¦Ó°ò¥»ÀW²v«h¬O 1/0.0091 = 110.25 Hz¡A´«ºâ¦¨¿ûµ^ªº«öÁä¡A«D±`±µªñ©ó¤¤¥¡ la ¡]¹ïÀ³ÀW²v¬O 440 Hz¡^­°16«×¡]¨â­Ó¥þ­µ¶¥¡^©Ò±o¨ìªº­µ¡A¤]´N¬O¤U¹Ï¤¤ªº A4¡]¥ªÃä¼Æ¨Ó²Ä 5 ­Ó¥ÕÁä¡^¡C


300

Hint
­Y­nª¾¹D¨C­Ó¿ûµ^«öÁ䪺Án­µ°ª§C¡A¥i¥H¹Á¸Õ³o­Óºô­¶¡G¡uhttp://www.oliphaunts.com/pianoroll-js¡v¡C

¤H¦Õ¹ïÁn­µ°ª§Cªº·Pı¡A¬O»P°ò¥»ÀW²vªº¹ï¼Æ¦¨¥¿¤ñ¡C¥H¿ûµ^ªºÁä½L¦Ó¨¥¡A¤¤¥¡ la ªºÀW²v¬O 440 Hz (Hertz)¡A°ª¤K«×ªº¤¤¥¡ la «h¬O 880 Hz¡A§C¤K«×ªº¤¤¥¡ la «h¬O 220 Hz¡C¿ûµ^¤¤ªº¨C¤@­Ó¥þ­µ¶¥¥]§t7­Ó¥ÕÁä»P5­Ó¶ÂÁä¡A¦@¦³ 12 ­ÓÁä¡A¥Nªí 12 ­Ó¥b­µ¡]semitones¡^¡A¥H MIDI ªº¼Ð·Ç¦Ó¨¥¡A¤¤¥¡ la ªº¥b­µ­È¬O 69¡A¹ïÀ³ÀW²v¬O 440 Hz¡A¦]¦¹¥b­µ©MÀW²v¤§¶¡ªºÂà´«µ{¦¡¥i¼g¦¨¤U¦C¤½¦¡¡G $$semitone=69+12\log_2\left(\frac{Hz}{440}\right).$$

¥Ñ¤@¬qÁn­µ¨Ó¨D¨ú¨ä­µ°ª¡A¦b­µ°T³B²z¤WºÙ¬°­µ°ª°lÂÜ¡]Pitch Tracking¡^¡A¬O­µ°T³B²z«Ü­«­nªº¤@Àô¡A¬ÛÃöÀ³¥Î¦³»y­µ¦X¦¨¡B­µ½Õ¿ëÃÑ¡B­ó°Û¿ïºqµ¥¡C

«e­±»¡©ú¹L¡Aaudioread ·|±NŪ¨ìªº­µ°TÀ£ÁY¦b -1 ©M +1 ¤§¶¡¡A¦ý¦pªG§A·QŪ¨ú­ì¨Ó­µ°TÀx¦s¦b­µ°TÀɮפ¤ªº­ì©l¸ê®Æ¡A¤]¥i¥H¡A½d¨Ò¦p¤U¡G

Example 6: 20-­µ°TŪ¼g¡B¿ý»s»P¼½©ñ/audioRead06.m[y, fs]=audioread('welcome.wav', [1, inf], 'native'); p=audioplayer(y, fs); play(p); % ¼½©ñ¦¹­µ°T time=(1:length(y))/fs; % ®É¶¡¶bªº¦V¶q plot(time, y); % µe¥X®É¶¡¶b¤Wªºªi§Î class(y) % Åã¥Ü y ªº¸ê®Æ«¬ºA ans = uint8

¥Ñ¤W¨Ò¥i¥H¬Ý¥X¡A·í§Ú­Ì¥[¤J 'native' ·í¦¨ audioread ªº²Ä¤T­Ó°Ñ¼Æ®É¡A©ÒŪ¥Xªº­µ°T´N·|¬O¥H­ì¨ÓÀɮפºªº¸ê®Æ«¬ºA¬°·Ç¡A¦]¦¹¥»¨Ò¤¤ªº­µ°T¸ê®Æ y ªº¸ê®Æ«¬ºA¬° uint8¡A¦P®Éµe¥Xªºªi§Îªº¾_´T½d³ò¤]¤£¦A­­©ó -1 ©M +1 ¤§¶¡¡C


MATLABµ{¦¡³]­p¡G¤Jªù½g