## Chapter 20: Exercises

1. (*)Obtain info from a mono audio file: Write a MATLAB script that can read the wave file "welcome.wav" and display the following information within this script.
1. Number of sample points.
2. Samping rate.
3. Bit resolution
4. Number of channels.
5. Time duration of the recording (in terms of seconds)
1. ­µ°T¸ê®ÆÂI¼Æ
2. ¨ú¼ËÀW²v
3. ¸ÑªR«×¡]¨C­Ó¨ú¼ËÂI¥Ñ¦h¤Ö¦ì¤¸¨Óªí¥Ü¡^
4. Án¹D¼Æ
5. ¿ý­µ®É¶¡ªø«×¡]¥H¬í¬°³æ¦ì¡^
3. (*)Obtain info from a stereo audio file: Repeat the previous exercise with a MATLAB program to obtain the same information from the wave file "flanger.wav".
4. ­«½Æ¤W¤@ÃD¡]µ{¦¡ÀÉ¦W¬° wavInfo02.m¡^¡A¦ý¬O±NÀÉ®×§ï¬° flanger.wav¡]¦¹ÀÉ®×¦ì©ó¥»®Ñ©Òªþ¥úºÐ¤§¤¤¡^¡C¤@¯ëªº CD ­µ¼Ö¬O±Ä¥ÎÂùÁn¹D¿ý­µ¡A¨ú¼ËÀW²v¬O 44.1 kHz¡A¸ÑªR«×¬O 16 bits/sample¡C½Ð°Ý¦b³o¼Ëªº±¡ªp¤U¡A¤@­º¥|¤ÀÄÁªººq­Y¬O¼g¦¨ wav ÀÉ®×¡A·|¤j¬ù¦û±¼¦h¤ÖµwºÐªÅ¶¡¡H¡]¥Ñ¦¹²ßÃD§A´N¥i¥Hª¾¹D MP3 ¡BWMA¡BAAC µ¥À£ÁY¥\¯àªº¥i¶Q¤F¡I¡^
5. ½Ð®Ú¾Ú¤U¦Cµ{¦¡½X¦^µª°ÝÃD¡C [y, fs]=wavread('welcome.wav'); wavplay(y,1.0*fs,sync); wavplay(y,1.2*fs,sync);
1. 'sync' ¥Nªí¤°»ò·N«ä¡H
2. ¦pªG§ï¬°§â²Ä¤@­Ó 'sync' §ï¬° 'async'¡A·|¦³¤°»ò®t§O¡H
Ans:
1. 'sync' ¥Nªí±N­µ°T¸ê®Æ´§Ç°e¨ì³â¥z¼½©ñ¡C
1. wavplay(y, 0.5*fs, 'async');
2. wavplay(-y, fs, 'sync');
3. wavplay(-y, -fs, 'sync');
4. wavplay(flipud(y), fs, 'sync');
1. sound(y, fs)
2. sound(y, fs*2)
3. sound(y*2, fs)
4. sound(y*2, fs*2)
8. MATLAB ¨Ï¥Î wavread Åª¤J wav ÀÉ®×¤§«á¡A·|±N­µ°T¸ê®ÆÀx¦s¦¨¦óºØ¸ê®Æ«¬ºA¡H¦b­þ­Ó°Ï¶¡¡H
1. int, [-10,10]
2. double, [-1,1]
3. int, [-32768,32767]
4. double, [-10,10]
9. wav ÀÉ®×¤º³¡Àx¦s­µ°T¸ê®Æªº¸ê®Æ«¬ºA¡A³Ì±¨£ªº¬O­þ¨âºØ¡H?¨ä¼Æ­È½d³ò¬°¦ó¡H
Ans:
1. uint8 (unsigned 8-bit integers), [0, 255]
2. int16 (signed 16-bit integer), [-32768, 32767]
Ans:
1. uint8 ==> (x-128)/128.
2. int16 ==> x/32768.
11. ¡u§Ö¼Ö¹|¡vÂ²ÃÐ¦p¤U¡G
¢²¢²¢³¢´¢x¢´¢³¢²¢±¢x¢°¢°¢±¢²¢x¢²¡D¢± ¢± ¡ß¢x

Ans: semitone=[60 62 64 65 67]; % Conversion from the simple score to semitone simpleScore=[3 3 4 5 5 4 3 2 1 1 2 3 3 2 2]; beat=[1 1 1 1 1 1 1 1 1 1 1 1 1.5 0.5 2]; fs=8000; signal=[]; for i=1:length(simpleScore); time=linspace(0, beat(i)/2, fs*beat(i)/2); f=440*2^((semitone(simpleScore(i))-69)/12); y=sin(2*pi*f*time); signal=[signal y]; end sound(signal, fs);
12. (*)Wave recording: Write a MATLAB script to record 10 seconds of your utterance such as "My name is Roger Jang and I am a senior student at the CS department of National Taiwan University". Save your recording as myVoice.wav. Other recording parameters are: sample rate = 16 KHz, bit resolution = 16 bits. Please use the script to print out answers to the following questions within the MATLAB window.
1. How much space is taken by the audio data in the MATLAB workspace?
2. What the data type of the audio data?
3. How do you compute the amount of the required memory from the recording parameters?
4. What is the size of myVoice.wav?
5. How many bytes is used in myVoice.wav to record overheads other than the audio data itself?
13. ½Ð¼g¤@¬q MATLAB µ{¦¡ recordMyVoice01.m¡A¿ý¤U§AÁ¿ªº¤@¬q¸Ü¡G¡u§Ú¬Oxxx¡A¤µ¦~xx·³¡A¬Oxx¤j¾Çxx¨tx¦~¯Åªº¾Ç¥Í¡v¡A¨Ã±N¤§Àx¦s¦¨ÀÉ®× myvoice.wav¡C¿ý­µ±ø¥ó¦p¤U¡G¿ý­µ®É¶¡¬O 10 ¬í¡A¨ú¼ËÀW²v¬O 11025 Hz¡A¸ÑªR«×¬O 8 bits/sample¡C½Ð°Ý¡G
1. ¬ÛÃöªº­µ°TÅÜ¼Æ¦b MATLAB ªº¤u§@ªÅ¶¡¦û±¼¦h¤Ö°O¾ÐÅé¡H
2. ¦¹ÅÜ¼Æªº¸ê®Æ«¬ºA¬O¤°»ò¡H
4. myvoice.wav ªºÀÉ®×¤j¤p¬°¦ó¡H
5. ÀÉ®×¥Î¤F¦h¤Ö¦ì¤¸²Õ¨Ó°O¿ý°£¤F­µ°T¸ê®Æ¥H¥~ªº¸ê°T¡H
15. (**)Create the illusion of a moving sound source: Record your own voice of "my name is xxx and I am a student at xxx university", and save the mono recording to myVoice.wav. Write a MATLAB script that can read the audio data from myVoice.wav, duplicate the audio data to create a stereo audio, and then modify the volume of each channels such that the playback can create an illusion that the sound source is moving between your two speakers. (Hint: You can observe the waveforms of the two channels in flanger.wav.)
16. (*)Reverse playback: Write a MATLAB script to accomplish the following tasks:
1. Record your utterance of "we" and play it backwards. Does it sound like "you"? (Please save the result to a wave file and demo its playback to the TA.)
2. Record your utterance of "you" and play it backwards. Does it sound like "we"? (Please save the result to a wave file and demo its playback to the TA.)
3. Record your utterance of "¤W®ü¦Û¨Ó¤ô¨Ó¦Û®ü¤W" (for Chinese students) or "We are you" (for international students) and play it backwords. What does it sound like? (Please save the result to a wave file and demo its playback to the TA.)
4. Record your utterance of "¤@¤G¤T¥|¤­¤»¤C¤K¤E¤Q" and play it backwards. Which digits can you recognize? Why?
5. Can you think of any other utterances that sound meaningful when played backwords?
18. (*)Audio signal manipulation: Write a MATLAB script to record your utterance of "today is my birthday". Try to explain the playback effect you observe after you try the following operations on the audio signals.
1. Multiply the audio signals by -1.
2. Reverse the audio signals in time axis.
3. Multiply the audio signals by 10.
4. Replace each sample by its square root.
5. Replace each sample by its square.
6. Clip the waveform such that sample data out of the range [-0.5, 0.5] are set to zero.
7. Modify the waveform such that samples in the range [-0.5, 0.5] are set to zero; samples out of the range [-0.5, 0.5] are moved toward zero by the amount 0.5.
19. (*)°ò¥»¿ý­µ: ½Ð¥Î MATLAB ¼g¤@¤p¬qµ{¦¡¡A¶i¦æ¿ý­µ¤T¬í¡A¿ý­µªº¤º®e¬O¡u¥xÆW¤j¾Ç¸ê°T¨t¡v¡A¨ä¤¤¨ú¼ËÀW²v¬O 16 KHz¡A¸ÑªR«×¬O 8 ¦ì¤¸¡A½Ð±N­µ°TÀx¦s¦¨ myVoice.wav ÀÉ®×¡C
1. ½Ð°ÝÀÉ®×¤j¤p¬°¦ó¡H
¡]´£¥Ü¡G·|¥Î¨ìªº«ü¥O¦³ wavrecord, wavwrite, flipud, sign, sound µ¥¡C¡^
20. (*)Audio signal grafting: Write a MATLAB script to accomplish the following tasks. (You need to find the boundaries by trials and errors, and put the related boundary indices into your MATLAB program for creating the required audio segments.)
• For Mandarin-speaking student: Record your utterance of "²MµØ¤j¾Ç¸ê°T¨t" and save it to a file first.
1. If you connect the consonant part of "¤j" to the vowel part of "¨t", can you get the sound of "¦a"? (Please save the result to a wave file and demo its playback to the TA.)
2. If you connect "¨t" to the vowel part of "¤j", can you get the sound of "¤U"? (Please save the result to a wave file and demo its playback to the TA.)
• For international students: Record your utterance of "keep it simple" and save it to a file.
1. Can you get the sound of "pimple" by connecting some portions of "Keep" and "simple"? (Please save the result to a wave file and demo its playback to the TA.)
2. Can you get the sound of "simplest" by connect some portions of your recording? (Please save the result to a wave file and demo its playback to the TA.)
21. (**)Experiments on the sample rate: Write a MATLAB script to record your utterance of "my name is ***" with a sample rate of 32 KHz and 8-bit resolution. Try to resample the audio signals at decreasing sample rates of 16 KHz, 8 KHz, 4 KHz, 2 KHz, 1 KHz, and so on. At which sample rate you start to have difficulty in understanding the contents of the utterance?
22. (**) Experiments on adding noise: Write a MATLAB script to record your utterance of "my name is ***" with a sample rate of 8 KHz and 8-bit resolution. We can add noise to the audio signals by using the following program snippet: k = 0.1; y2 = y + k*randn(length(y), 1); % Add noise sound(y2, 8000); % Playback plot(y2); Increase the value of k by 0.1 each time and answer the following questions.
1. At what value of K you start to have difficulty in understanding the content of the playback?
2. Plot the waveforms at different values of k. At what value of k you start to have difficulty in identifying the fundamental period of the waveform?
23. (**)Voice signal encryption: Write a function that can take a wave file, encrypt it, and save it as another wave file. The I/O format is
myEncrypt(inputFileName, outputFileName);

where "inputFileName" is a string specifying the input wave file, and "outputFileName" is a string specifying the output wave file. The encryption process is like this (assuming y is the original signal and z is the encrypted signal):
1. z=y;
2. if y(i)>0, z(i)=1-y(i) for all i
3. if y(i)<0, z(i)=-1-y(i) for all i
4. z=flipud(z);
Note that:
• The encrypted file can be converted to the original file using the same function.
• Be aware that this is a naive encryption; better methods exist.
24. (*)Create Sine waves:
1. Create a sine wave of 4-second duration and 440-Hz frequency, with a sample rate of 16000.
2. Create a sine wave of 4-second duration, 16000-Hz sample rate, with a frequency linearly varying from 0 to 800 Hz.
25. (**)Generate sine wave with time-varying frequencies: Write a function to generate a sine wave with time-varying frequencies. The I/O format is
outputSignal=mySine(duration, freq);

where freq is a two-element vector [f1, f2], indicating the frequency of the sine wave should change linearly from f1 (for the first sample point) to f2 (for the last sample point). Note that
• The sample rate is 16 KHz.
• The first sample is zero, starting from time 0. (In other words, the time vector is (0:duration*fs-1)/fs, and the function to invoke is "sin".)
Note that he instantaneous frequency of $y=sin(2\pi\phi(t))$ is $\phi'(t)$. Therefore you can use the following conditions to find $\phi(t)$: $$\left\{ \begin{matrix} \phi'(t)=at+b,\\ \phi'(0)=f_1,\\ \phi'(duration-1/fs)=f_2,\\ sin(2\pi\phi(0))=0.\\ \end{matrix} \right.$$
Hint: Here is a snippet for generating a 3-second sine wave of 440Hz: duration=3; f=440; fs=16000; time=(0:duration*fs-1)/fs; y=sin(2*pi*f*time); plot(time, y); sound(y, fs);
26. (*)Create sirens: Use combination of sine waves to generate the sound of sirens (that you usually hear from an ambulance). Here is an example.
27. (**)Create beats:
1. Create a sine wave y1 of 3-second duration and 440-Hz frequency, with a sample rate of 16000.
2. Create a sine wave y2 of 3-second duration and 444-Hz frequency, with a sample rate of 16000.
3. Try "sound(y1+y2, fs)", what do you hear? Please use math derivation to explain such phenomenon of "acoustic beats". What is the frequency of the beats in this case?
28. (**)Create time-varying beats:
1. Create y1 as the previous exercise.
2. Create y2 which has a time-varying frequency from 440-Hz to 500-Hz (which changes linearly with time).
3. Try to play y1+y2 and explain what you hear.
29. (**)Resample audio signals: Write a MATLAB script to resample the audio signals in "sunday.wav" such that new waveform has a new sample rate of 11025. Plot these two waveform in the suplot(2, 1, 1). Plot their absolute difference in subplot(2, 1, 2).
°ß¤@¤£¦PÂI¡A¬O©Ò¶Ç¦^¨Óªº­µ°TÅÜ¼Æ y ¬O¾ã¼Æ­È¡A¦pªG nbits ¬O 8¡A«h y ªº½d³ò¥²¶·¤¶©ó -128 ¨ì 127 ¤§¶¡¡F¦pªG nbits ¬O 16¡A¨º»ò y ªº½d³ò´N·|¤¶©ó -32768 ¦Ü 32767 ¤§¶¡¡C ¡]´£¥Ü¡G§A¥²¶·¥ýÁA¸Ñ wavread() ªº¥Îªk¡C¡^
31. (**)¦p¦ó«Ø¥ß­µ®Ø: ½Ð¼g¤@­Ó¨ç¼Æ buffer2.m¡A¥Îªk¦p¤U
framedY = buffer2(y, frameSize, overlap);
>> y=[1 2 3 4 5 6 7 8 9 10 11 12 13 14]; >> buffer2(y, 4, 1) ans = 1 4 7 10 2 5 8 11 3 6 9 12 4 7 10 13
¥t¡A½Ð°Ý³o­Ó¨ç¼Æ buffer2.m ©M Signal Processing Toolbox ¤¤ªº buffer ¨ç¼Æ¦³¦ó¤£¦P¡H
32. (*)¨ú¼ËÀW²vªº¼vÅT: ½Ð¥Î MATLAB ¼g¤@¤p¬qµ{¦¡¡A¶i¦æ¿ý­µ¨â¬í¡A¿ý­µªº¤º®e¬O¡u§Ú¬O¬Y¬Y¬Y¡v¡A¨ä¤¤¨ú¼ËÀW²v¬O 32 KHz¡A¸ÑªR«×¬O 8 ¦ì¤¸¡A½Ð±N­µ°TÀx¦s¦¨ myVoice2.wav ÀÉ®×¡C½Ð¹ï°T¸¹¶i¦æ­«·s¨ú¼Ë¡]Resample¡^¡AÅý¨ú¼ËÀW²vÅÜ¦¨ 16 KHz, 8 KHz, 4 KHz, 2 KHz ...µ¥µ¥¡A½Ð°Ý·í¨ú¼ËÀW²v±¼¨ì¦h§C®É¡A§A¤w¸gÅ¥¤£¥X¨Ó­ì¥ýªºÁn­µ¡H
33. (*)Âø°Tªº¼vÅT: ½Ð¥Î MATLAB ¼g¤@¤p¬qµ{¦¡¡A¶i¦æ¿ý­µ¨â¬í¡A¿ý­µªº¤º®e¬O¡u§Ú¬O¬Y¬Y¬Y¡v¡A¨ä¤¤¨ú¼ËÀW²v¬O 8 KHz¡A¸ÑªR«×¬O 8 ¦ì¤¸¡C°²³]­µ°T°T¸¹¬O¦s¦b¤@­Ó¦æ¦V¶q y¡A§Ú­Ì¥i¥H¥H¤U¦C¤è¦¡¥[¤JÂø°T¡G
k = 0.1; y2 = y + k*randn(length(y), 1); % ¥[¤JÂø°T sound(y2, 8000); % ©ñ­µ plot(y2);
·í k ­È¥Ñ 0.1¡B0.2¡B0.3 µ¥ºCºC¼W¤j®É¡AºCºC§AªºÁn­µ·|¶V¨Ó¶V¼Ò½k¡C
1. ½Ð°Ý¨ì k ­È¬O¦h¤Ö®É¡A§A·|Å¥¤£¥X¨Ó­ì¥ýÁ¿¸Üªº¤º®e¡H
2. ½Ð°Ý¨ì k ­È¬O¦h¤Ö®É¡A§A·|¬Ý¤£¥X¨Ó y2 ¥]§t¤@¬qÁn­µªº°T¸¹¡H¡]´«¨¥¤§¡A¦b©ñ¤j y2 ªº¹Ï§Î«á¡A§A¤w¸g¬Ý¤£¥X¨Ó¦³°ò¥»¶g´Áªº¦s¦b¡C¡^
34. (**)­«·s¨ú¼Ë: ½Ð¥Î interp1 «ü¥O¡A¹ï myVoice2.wav ªº­µ°T¶i¦æ­«·s¨ú¼Ë¡AÅý¨ú¼ËÀW²vÅÜ¦¨ 11025 Hz¡A¨Ã±Nµ²ªGÀx¦s¦¨ myVoice3.wav¡C