120 likes | 217 Vues
Lab 3. Signals in Matlab. Signals in Matlab. Lab objectives Generate a signal which is the sum of two sinusoids of different frequencies, of a level and sample rate suitable to send to the soundcard. Plot it. Add noise to the above signal, and plot the result.
E N D
Lab 3 Signals in Matlab
Signals in Matlab Lab objectives • Generate a signal which is the sum of two sinusoids of different frequencies, of a level and sample rate suitable to send to the soundcard. Plot it. • Add noise to the above signal, and plot the result. • Write it to a .wav file for playback
Signals in Matlab Let’s pick some parameters. We want to keep the .wav file reasonably compact, so let’s use monaural 8-bit format, and a sampling rate of fs = 8 kHz. The file will be slightly more than 8 kB for each second of signal. The maximum signal amplitude we can write to a .wav file is 1, so we’ll make our sinusoids have an amplitude of 0.2, or an RMS level of 0.141.
Signals in Matlab We’ll pick a noise level that’s audible, but below the signal value. The noise variance will be 0.1. We’re sampling at fs = 8000 samples/sec, so the sinusoids must each be less than half that frequency. We’ll pick 1,000 Hz. And 1500 Hz. You’ll be handing in an m-file which generates and plots these signals, and writes a .wav file. You’ll turn in a second m-file that reads the .wav file.
Signals in Matlab Let’s generate 5 seconds worth of signal, so the first step is to generate a time vector representing 5 seconds at 8000 samples/second. The vector’s length will be: t = linspace(0, 5, 40000);
Signals in Matlab Next, generate the 1000 Hz sinusoid: s1 = A1 * sin(2 * pi * 1000 * t) Where A1 is a constant that sets the amplitude of the sinusoid Generate s2, the 1500 Hz sinusoid similarly
Signals in Matlab Next, sum the sinusoids together. Call the resultant “s”, and plot 128 samples of it. Label the axes (see how this was done in Lab 1). Now, generate the noise. n = randn(size(t)) Will generate a vector of 0-mean, unit variance, normally distributed gaussian noise, the same size as the vector t. In other words, 5 seconds of Gaussian noise!
Signals in Matlab We want the noise variance to be half of the signal power (3 dB below the signal), so n = .1 * randn(size(t)); Will do the job. Add this to the sum of the two sinusoids, and plot it.
Signals in Matlab Next, we’ll write it to a .wav file. In Matlab, use wavwrite(sn, 8000, 8, ‘test.wav’) Where sn is the vector you’ve created by summing the two sinusoids with the noise.
Signals in Matlab In octave, ausave(‘test.wav’, sn’, 8000, ‘short’) Will do the same thing. You’ll have to download ausave.m (link on the website) and place it in the “site/m” folder.