EC312 Security Exercise 17 – Analog to Digital Conversion All signals found in nature are analog – they’re smooth and continuously varying, from the sound of an orchestra to the acceleration of your car to the clouds moving through the sky. An excerpt from http://www.netguru.net/ntc/NTCC5.htm provides some additional background on analog signals: The term "analog" comes from the word "analogous" meaning something is similar to something else. It is used to describe devices that turn the movement or condition of a natural event into similar electronic or mechanical signals. There are numerous examples, but let's look at a couple. A non-digital watch contains a movement that is constantly active in order to display time, which is also constantly active. Our time is measured in ranges of hours, minutes, seconds, months, years, etc. The display of a watch constantly tracks time within these ranges. In effect the data represented on a watch may have any number of values within a fairly large range. The watch's movement is analogous to the movement of time. In this respect the data produced is analog data. Another prime example of an analog device is a non-digital thermometer measuring a constantly changing temperature. The action is continuous and the range is not very limited, though sometimes we wish it were. The data produced by a thermometer is analogous to the change in temperature. Therefore, it is an analog signal. Analog signals most closely represent the natural form of information as closet vinyl enthusiasts would agree: http://brokensecrets.com/2010/02/19/why-vinyl-records-are-becoming-popularagain/ If analog recordings provide superior sound quality, why has the world gone digital? There’s a major disadvantage to the analog format: Noise. Digital signals, on the other hand, have several advantages, including noise immunity. In this Security Exercise, we’ll explore the process of converting analog signals to digital signals. Set-up. Equipment required: □ Your issued Laptop □ MATLAB ( if you don’t already have it on your laptop) o o o □ Go to http://intranet.usna.edu/IRC/software/softwareList.htm. Scroll 2/3 of the way down the web page to the MATLAB section. Download and install MATLAB in accordance with the installation instructions. MATLAB files and code for the Analog to Digital lab o Download the MATLAB files and code for SX17 from the location specified by your instructor and place in a folder called a2d-MATLAB on your Desktop. Part I: Introduction A simple sine wave representative of an analog tone is shown below. Question 1: What is the period of this signal? Question 2: What is this signal’s frequency? (Show your work!) Question 3: What does this signal look like in the frequency spectrum? In order for this sine wave to be converted to a digital signal, it must be sampled (measured at regular time intervals), quantized (divided into levels), and encoded (assigned binary values to each sample). In order for the original signal to be recreated in digital form, it must be sampled at at least the Nyquist Sampling Rate (fN), or twice the highest frequency component of the signal. Question 4: Solve for fN. (Show your work!) If this signal is sampled at less than fN, aliasing will occur. Aliasing is the distortion that results when the signal reconstructed from samples is different from the original continuous signal. Think of it as connect-the-dots: Each time the signal is sampled, a dot represents a snapshot of the signal at that point in time. When the signal is reconstructed using Digital to Analog Conversion (which is the opposite of Analog to Digital Conversion), the converter plays a game of connect-the-dots… And if there aren’t enough dots to give the converter a good idea of the original signal, then the original signal is essentially lost. 2 Question 5: For the sampled signals below, which gives the closest approximation of the original signal? National Instruments: Analog Sampling Basics, June 5, 2013 Here’s an example of a 200Hz sine wave, sampled at 10 kHz and at 2 kHz. Time domain representation of (a) 200 Hz continuous sine wave (b) sampled at 10 kHz and (c) sampled at 2 kHz http://www.intechopen.com/books/data-acquisition-applications/subsampling-receivers-with-applications-to-software-defined-radio-systems Part II: Sampling a Tone The MATLAB code provided by your instructor allows you to adjust the sampling frequency (fs) for an “analog” file you select. [Note: We have “analog” in quotes, because our original signal isn’t actually analog in this case but is actually a digital signal with a very high sampling rate. For the purposes of this lab, that’s “close enough” to analog.] □ Open the provided ADC lab code (soundcard_adc_lab2013b.m) in MATLAB by double clicking the soundcard_adc_lab2013b.m file in the a2d-MATLAB folder you created on your desktop. Briefly review the program. (You don’t have to be able to reproduce it, but you should have a general understanding of what it does.) Question 6: Based on the comments provided in the code, what does the soundcard_adc_lab2013b.m program do? What variable will you be manipulating? 3 The sampling code loads an analog-like audio file, which you can select by uncommenting (removing the % from) the beginning of the line of the selected file in the “Load ‘Analog’ Audio File” section, reproduced below. Initially, the code is set up to sample a 4 kHz tone. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Load "Analog" Audio File % % The following variables are loaded into the workspace: % % audio -- The audio samples % % time -- The corresponding timevector % % ADD/REMOVE COMMENT (%) ONLY WHEN DIRECTED % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% load('4_kHz_tone.mat'); %load('Lorde_Royals.mat'); %load('Imagine_Dragons_Radioactive.mat'); %load('Blink_182_Midnight.mat'); %load('Lady_Gaga_Applause.mat'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Question 7: What is the minimum sampling frequency (fs) that should be used to sample the 4 kHz tone without aliasing? □ Listen to the 4 kHz audio file by typing load(‘4_kHz_tone.mat’) into the MATLAB command window, pressing <enter>, and then typing soundsc(audio,96000) and pressing <enter> as shown below. □ You should hear several seconds of (an annoying) 4 kHz tone through your laptop speakers. If you don’t, adjust your speaker settings so you can hear the 4 kHz tone. Adjust the sampling frequency in your MATLAB code to the value obtained in Question 7. □ □ Run your code (i.e. the file soundcard_adc_lab2013b.m) to sample the tone at the specified frequency. After approximately 5 seconds, a display of the signal in the time domain and frequency domain will appear. 4 □ Use the marker in the “Figure 1” window to determine the frequency of the sampled signal from the (bottom) frequency domain plot. Question 8: Is the frequency of the sampled signal still 4 kHz? □ Play back your sampled signal by typing soundsc(sampled_audio,fs) in the MATLAB Command Window. You should hear the samples you captured. Question 9: Does the sampled tone you played sound the same as your original tone? Why or why not? Question 10: Is the (top) time domain plot a good representation of your original 4 kHz sine wave? Question 11: Count to determine how many times the sine wave was sampled per period. [Note: Be careful here. You should get the same answer whether you count “peak-to-peak” or “zero-crossing to zerocrossing”.] □ □ In order to capture a better time domain representation of the tone, change your sampling frequency to 48 kHz. Run your code to sample the tone at your new sampling frequency. Question 12: Is the time domain plot a better representation of your original 4 kHz sine wave? Question 13: Count to determine how many times the sine wave was sampled per period. Does this make sense with your new sampling frequency? Why or why not? □ Now change your sampling frequency to 6 kHz. Question 14: Is your new sampling frequency greater than fN? What phenomenon will result? □ □ Run your code to sample the tone at your new sampling frequency. Type soundsc(sampled_audio,fs) in your MATLAB command window to play it back. Question 15: Can you hear the difference between your sampled signal and the 4 kHz analog tone? What frequency does your undersampled signal appear to be? (Hint: Use to determine the frequency from the frequency domain plot.) 5 Part III: Sampling a Tune Since listening to a 4 kHz tone is about a much fun as watching paint dry, let’s try something more practical. Your soundcard_adc_lab2013b.m program also includes selections from Lorde, Imagine Dragons, Blink 182, and Lady Gaga. (Yes, we ECE professors are hip to what the kids are listening to these days.) □ Select your favorite artist by adding a comment (%) to the beginning of the 4 kHz tone and removing the comment from your selected tune. For example, to listen to Imagine Dragons, do the following: Add % Remove % □ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Load "Analog" Audio File % % The following variables are loaded into the workspace: % % audio -- The audio samples % % time -- The corresponding timevector % % ADD/REMOVE COMMENT (%) ONLY WHEN DIRECTED % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %load('4_kHz_tone.mat'); %load('Lorde_Royals.mat'); load('Imagine_Dragons_Radioactive.mat'); %load('Blink_182_Midnight.mat'); %load('Lady_Gaga_Applause.mat'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Change the commented line in the “Plot time and frequency” section of the MATLAB code so that the first “axis” line has a % at the beginning and the second does not. (As shown below.) Add % Remove % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plot time and frequency % % CHANGE AXIS "COMMENT" (%) ONLY WHEN DIRECTED % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [freq, power] = get_spectrum(sampled_audio, fs); figure subplot(2,1,1) plot(sampled_time,sampled_audio,'ko-') xlabel('Time (sec)') ylabel('Amplitude') %axis([2 2.00226 -.5 .5]) axis([2 2.2 -.5 .5]) title('Sampled Signal vs. Time and Frequency') subplot(2,1,2) plot(freq/1e3,power,'k-') xlabel('Frequency (kHz)') ylabel('Relative Power (dB)') axis([0 20 -60 1]) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The generally accepted standard range of audible frequencies is 20 to 20,000 Hz, so let’s assume that the highest frequency in whatever you chose as your “groove” is 20 kHz. Question 16: What is the minimum sampling frequency at which you should sample your music selection in order to avoid aliasing? Sample it at 48 kHz (close enough!) and run your code. From the frequency domain plot, what is the highest frequency component in this sound signal? 6 Play the sampled version by typing soundsc(sampled_audio,fs) in your MATLAB command window to play it back. Since you sampled at higher than the Nyquist frequency, the sampled version should sound right. Choose a frequency that will result in aliasing, i.e. so that your music will be undersampled. (Note, because of the way the MATLAB code is written, the only frequencies you can choose are integer factors of 96 kHz, i.e. 96/N kHz, where N is an integer.) The lower the frequency, the more pronounced your aliasing effects will be. Run your code with the new sampling rate, and then play the resulting sampled music. Question 17: What effects of aliasing do you see in the frequency domain representation of your signal? How does the signal sound different? In reality, most CD-quality digital audio systems sample at a minimum of 44.1 kHz and use 16-bit quantization. DVD-quality audio samples at 96 kHz and uses 24-bit quantization. Question 18: How many quantization levels result from a 16-bit quantizer? A 24-bit quantizer? Question 19: Suppose the samples of your song are depicted by the image below. If a system uses an 8bit quantizer, how many bits represent each sample, and how many quantization levels result? Question 20: What does the red outline in the figure above represent? Question 21: If the 1st sample falls into level 0, the 2nd sample falls into level 150, the 3rd sample falls into level 192, the 4th sample falls into level 131, and the 5th sample falls into level 84, what is the bit stream that represents the beginning of your signal (in binary)? Assoc. Prof. Chris Anderson and LCDR Jennie Wood 7 EC312 Security Exercise 17 Name: __________________________________________________________________________________________ Question 1: __________________________________________________________________________________________ Question 2: __________________________________________________________________________________________ Question 3: __________________________________________________________________________________________ Question 4: __________________________________________________________________________________________ Question 5: __________________________________________________________________________________________ Question 6: __________________________________________________________________________________________ Question 7: __________________________________________________________________________________________ Question 8: __________________________________________________________________________________________ Question 9: __________________________________________________________________________________________ Question 10: __________________________________________________________________________________________ Question 11: 8 __________________________________________________________________________________________ __________________________________________________________________________________________ Question 12: __________________________________________________________________________________________ Question 13: __________________________________________________________________________________________ Question 14: __________________________________________________________________________________________ Question 15: __________________________________________________________________________________________ Question 16: __________________________________________________________________________________________ Question 17: __________________________________________________________________________________________ Question 18: __________________________________________________________________________________________ Question 19: __________________________________________________________________________________________ Question 20: __________________________________________________________________________________________ Question 21: __________________________________________________________________________________________ 9