ELEC 241 Lab

Experiment 8.2

Generating Signals Digitally

Part 1: Periodic Signals

In Lab 6 we used a Labview function generator to produce waveforms. We can do the same with a Matlab program.


Step 1:

Since we're going to be looking at the functions as time signals, let's start by creating a time base, a vector containing the value of t at each sample interval in our four second signal.

   >> time = [0:39999] / Fs;
   >> plot(time)


Step 2:

We can generate a 200 Hz sine wave:

   >> wave=sin(2*pi*200 * time);
   >> sound(wave,Fs)


Step 3:

We can also generate square waves:

   >> wave=square(2*pi*200 * time);
   >> sound(wave,Fs)


Step 4:

We could go on down the list, doing triangle waves, narrow pulses, etc., but let's try something more interesting.

   >> wave=chirp(time,0,4,5000);
   >> sound(wave,Fs)
   >> specgram(wave,256,Fs)


Question 3:

Write a formula that describes this signal as a function of time.

Step 5:

What use is such a signal? Well, it gives us another way to plot frequency response. The vectors $b$ and $a$ should still contain the coefficients of our elliptic filter.

   >> freqz(b,a)

Apply this filter to our chirp signal and look at the output

   >> out1=filter(b,a,wave);
   >> plot(out1)

Note that the outline or envelope of the filter output in the time domain is the same shape as the magnitude of the response in the frequency domain. Since freqz produces a log plot, we can make this correspondence clearer by taking the log of the filter output:

   >> plot(20*log10(abs(out1)))


Step 6:

This is a fairly roundabout way to get frequency response in Matlab, but it can be quite useful in analog circuits where we don't have freqz(). Add a 10 k resistor and a 100 nF capacitor to your earphone driver circuit as shown below.
\includegraphics[scale=0.500000]{ckt8.1.ps}

Note that this is a simple first order low pass filter. Connect CH1 of the scope to $v_{out}$ .

Step 7:

If the scope could sweep slowly enough, we could get by with a single chirp, like we used for the digital filter. However, since the analog scope has to be continuously refreshed, we need a repetitive chirp. We can get this by concatenating a bunch of short chirps:

   >> tweet=chirp(time(1:1000), 0, 0.1, 5000);
   >> test=0
   >> for i=1:40
   >> test=[test tweet];
   >> end


Step 8:

Set the TIME/DIV switch on the scope to 10 mSEC. Play the test waveform and adjust the CH1 VOLTS/DIV so that the display fills the screen (about 20mV).

Step 9:

Now set the SWEEP MODE to NORMAL and play the waveform. If necessary, adjust the Trigger LEVEL control for a stable trace. The envelope of the displayed waveform should correspond to the magnitude of the frequency response of the filter.

Step 10:

Convert the filter to a high pass filter by exchanging the resistor and capacitor, and repeat the previous step. It may be difficult to sync the scope, but you should be able to see the response.

Part 2: Random Signals

In addition to deterministic, periodic signals, we can also generate random (actually pseudo-random) signals, usually referred to as noise signals.


Step 1:

Generate a noise signal.

   >> noise=randn(size(sig1));


Step 2:

Listen to it and plot its spectrogram.

Diversion:

In real communication systems, one of the bad things that happens to a signal is that it is corrupted by additive noise. We can model this process in the lab with our synthetic noise signal.

A measure of how badly the signal is corrupted is the signal-to-noise ratio or SNR. This is the ratio of the mean-square value of the signal to the mean-square value of the noise, expressed in dB.

$\displaystyle SNR=10 \log_{10}\left(\frac{\frac{1}{T}\int_0^T s(t)^2 dt}{\frac{1}{T}\int_0^T n(t)^2 dt}\right)$


Step 3:

Find the mean-square value of the reference signal and the noise signal.

   >> psig=mean(sig1 .^ 2)
   >> pnoise=mean(noise .^ 2)


Step 4:

Create a noisy signal by adding a multiple of the noise to the reference signal.

   >> An = 0.1
   >> out1 = sig1 + An*noise;
   >> sound(out1, Fs)


Step 5:

Increase $An$ until you can no longer understand what is being said. What is the threshold SNR?

Step 6:

The SNR you measured in the last step is a bit misleading. Since computing the mean-square value of the entire reference signal includes some regions of silence, the reference signal energy is underestimated. To get a better estimate, select a representative chunk from the speech signal and compute its mean-square value. What is your new estimate of SNR?

Step 7:

As you saw from its spectrogram, the spectrum of our noise signal is constant with frequency. I.e. it is wideband, white noise. We can make narrow band, colored noise by passing white nose through a bandpass filter.

   >> out1=filter(b2, 1, noise);

Listen to this signal and plot its spectrogram.