ec312 – sx17 - analog to digital conversion · %load('lady_gaga_applause.mat'); %%%%%...

12
EC312 SX17 - Analog to Digital Conversion 1 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 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-popular- again/ 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 Practical 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) MATLAB files and code for the Analog to Digital lab o Download the MATLAB files and code for PE17 from your instructor’s Google share drive lab Folder and place in a folder called a2d-MATLAB on your Desktop.

Upload: phunghanh

Post on 26-Jul-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

1

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 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-popular- again/

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 Practical 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) □ MATLAB files and code for the Analog to Digital lab

o Download the MATLAB files and code for PE17 from your instructor’s Google share drive lab Folder and place in a folder called a2d-MATLAB on your Desktop.

Page 2: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

2

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 domain?

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 greater than the Nyquist Sampling Rate (fN), which is 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.

Page 3: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

3

http://zone.ni.com/images/reference/en-XX/help/371361G-01/loc_eps_aliasing_effects.gif

Part II: Sampling a Tone

We’re going to use MATLAB, which is a high-level programming environment – think of it as your calculator on some heavy-duty performance enhancing drugs. It’s similar to C, but MATLAB’s GUI makes it more user- friendly for our purposes. The MATLAB code provided allows you to adjust the sampling frequency (fs) for an ‘analog’ file you select.

□ 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.)

%%%%%%%%%%%%%%%%% % SAMPLING CODE % %%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Clear out memory and initialize default settings % % DO NOT CHANGE THIS SECTION % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all close all set(0,'DefaultAxesFontSize',20) set(0,'DefaultAxesFontWeight','Bold') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Page 4: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Setup major variables % % CHANGE FS (ONLY) TO CHANGE SAMPLING FREQUENCY % % Note fs = 96000/N, where N is an INTEGER % % Valid fs = 48 kHz, 32 kHz, 24 kHz, etc. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% fs = 48e3; % Sampling frequency in Hz %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Get audio samples % % DO NOT CHANGE THIS SECTION % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [sampled_time, sampled_audio] = sample(time,audio,fs); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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]) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Page 5: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

5

Question 5: What does the soundcard_adc_lab2013b.m program do? What variable do you manipulate? 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 6: What sampling frequency (fs) must be exceeded for the 4 kHz tone in order to ensure that aliasing is always avoided?

□ 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 the 4 kHz tone through your laptop speakers. If you don’t, adjust your speaker settings so you can hear the 4 kHz tone.

Page 6: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

6

□ Adjust the sampling frequency in your MATLAB code to the value obtained in Question 6.

When the program obtains 5 seconds of samples, a display of the signal in the time domain and frequency domain will appear.

□ Use the marker in the “Figure 1” window to determine the frequency of the sampled signal from the (bottom) frequency domain plot.

Question 7: 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 (as shown). You should hear the samples you captured.

□ Press to sample the tone.

Page 7: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

7

Question 8: Does the sampled tone you played sound the same as your original tone? Why or why not?

□ If your sampled signal did not display 10 or fewer periods, use the zoom icon in the “Figure 1” window to zoom in on the time domain plot.

Question 9: Count to determine how many times the sine wave was sampled per period.

□ Now change your sampling frequency to 48 kHz.

□ Press to sample the tone at your new sampling frequency.

Question 10: 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.

Page 8: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

8

Question 11: Is your new sampling frequency greater than fN? What phenomenon will result?

□ Press to sample the tone at your new sampling frequency and type soundsc(sampled_audio,fs) to play it back.

Question 12: 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.)

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.

Select your favorite artist by adding a comment (%) to the beginning of the 4 kHz tone and removing the comment from your selected tune.

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.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plot time and frequency % % CHANGE AXIS "COMMENT" (%) ONLY WHEN DIRECTED % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [freq, power] = get_spectrum(sampled_audio, fs); figure

Page 9: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

9

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)')

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 13: What sampling rate should be exceeded in order to avoid aliasing?

□ Sample your music at 48 kHz (close enough) and play it back by typing soundsc(sampled_audio,fs) Does it sound “right?”

Question 14: Undersample your song (so that aliasing occurs): 8 kHz should do it. Play it back & compare the frequency content to the previous plot. How is it 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 15: How many quantization levels result from a 16-bit A/D converter? A 24-bit A/D converter?

Question 16: Suppose the samples of your song are depicted by the image below. If a system uses an 8- bit quantizer (A/D converter), how many bits represent each sample, and how many quantization levels result?

Page 10: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

10

Question 17: 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)?

Question 18: What is the significance of the bit stream obtained in Question 17?

Question 19: What does the red outline in the figure below represent?

Assoc. Prof. Chris Anderson and LCDR Jennie Wood

Page 11: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

11

Name:

Question 1:

Question 2:

Question 3:

Question 4:

Question 5:

Question 6:

Question 7:

Question 8:

Question 9:

Question 10:

Question 11:

Page 12: EC312 – SX17 - Analog to Digital Conversion · %load('Lady_Gaga_Applause.mat'); %%%%% Question 6: What sampling frequency (f s) must be exceeded for ... EC312 – SX17 - Analog

EC312 – SX17 - Analog to Digital Conversion

12

Question 12:

Question 13:

Question 14:

Question 15:

Question 16:

Question 17:

Question 18:

Question 19: