0% found this document useful (0 votes)
388 views7 pages

Signal Lab 3,4 2 PDF

Uploaded by

Mohamad Nabih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
388 views7 pages

Signal Lab 3,4 2 PDF

Uploaded by

Mohamad Nabih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Ab Ly Alexandria University ~ Dept. of Elec. Eng Third Year - Communications — Signal Processing using Matlab Lesson 3 Dealing with Sound Signals 3.1 Introduction In this lesson, we will explain how we deal with a common type of signal: sound signals. First of all, we will explain how we could load a sound fife into the Matlab workspace. Aftenwé ards, we will ‘explain how sound signals are represented and stored. We will then explain may be played or written to wave files. Finally, we will demonstrate the application of some signa processing operations to sound signals. 3.2 Importing Sound Signals Sound signals may be imported using two methods. The first method is using the import wizard. ‘One way of accessing the import wizard is through the file menu as depicted below. Fie Eat Dek Desks Whee Hee aa [ Chess ih locte : Mefilo Figure 3.1 You will then browse until you reach the desired file, select it, then click "open", This method is common for importing sound, images, video, or data files. However, you can only load sound files of type .wav or .au. The second method uses specialized sound importing functions. There are specialized functions for loading files-of type .wav and .au that come with the Matlab package, There are also Many toolboxes on the internet that give you files that could load other common sound file types, ‘such as mp3. You can reach the MP3 toolbox by googling the following string "MATLAB Central File Exchange - |MPSWRITE and MPSREAD". Now, we have two functions to use to load wave files and mp3 files: “wavread” and “mp3read". Wee will not deal with .au files because they are only used in Linux. In order to use wavread or ™p3read, the file we want to load must be situated in a folder that exists on the Matlab search Path. You can just put the file in the "Work" directory of Matlab, or you can go to Set Path in the file menu and the locations of your files. The wavread function may be used with a variable number of input and output arguments. The following instructions show various calls to wavread. >>Yewavread (' FileName") ; >> [¥, Fs] > (¥, Ps, Nits. >> [¥, Fs] > [¥, Fs] swavread(‘PileName', (N1 N2]}; Fonneaer | Renee? Tenreeen ] J | Alexandria University ~ Dept. of Elec. Eng. Third Year - Communications +_The first instruction shows the minimal form of using wavread. The input argument ‘FileName’ is a string containing the name of the wave file to be imported. The output argument is the variable in which the sound data will be stored. The second instruction features an additional output argument. When there are two or more output arguments, the second output argument will convey the sample rate of the wave file. The third instruction features a third output argument. This output argument conveys the number of bits per sample usod in storing the file. In the fourth and fifth instructions, we have added a second input argument. If this input is a scalar (N), as in the fourth instruction, Matlab will load the first N samples from each channel of the file. If this input is a two element vector ([N1 N2]}, Matlab will load the samples number N1 through N2 from each channel of the file. This may be useful for loading only a small part of a large wave file. 3.3 Representation of Sound Signals in Matlab in Matlab, a sound signal may either be a vector (row or column), or a two-column matrix. When, the sound signal is a vector, the sound is monophonic. When the sound signal is a two-column matrix, the sound is stereophonic. The left column contains the samples comprising the left speaker signal. The right column contains the samples comprising the right speaker signal. The number of elements per channel is equal to the duration of the file multiplied by the sample rate of the signal. The values of the samples lie in the range from -1 to 1. 3.4 Playing and Writing Sound Signals q When you process a sound signal, you will normally want to play the new Signal to evaluate the performance of your code. You may play a sound signal using the “sound” function, This function has the syntax shown below. >>sound (x, Fs) ; The first input argument x is the sound signal to be played. It may be monophonic or Stereophonic. The second input argument Fs is the sample rate of the sound signal. You may try Playing a sound signal with a sample rate that is less than or greater than its original sample rate. However, you will discover that the sound signal sounds slower or faster than normal. After you finish processing the signal, you might want to save the signal to a wave file. You may do so by using the ‘wavwrite* function. If you have downloaded the mp3 toolbox, you may also use the “mp3write" function to write the sound signal to an mp3 file, The following instruction shows the ‘syntax of using "wavwrite’. >>wavwrite (SoundSignal, Ps, 'OutputFileName) ; 3.5 Example of Processing Sound Signals In the following example, we will load a wave file, process its sound signal, and then write the result in a new wave tlle. We want to extract the first 4 seconds of the wave file, and apply a fade- in and a fade-out effect. The sound should fade in in two seconds, and then fade out in two Seconds. The file is assumed to be stereophonic and to have an initial duration longer than 4 Seconds. We will list the instructions and then explain how they work. seconds only (0,2, 2488) ; inspace (1,0,2*Fs) ; fading In >o¥(1:2*Fs,1 >>¥(1:2*Ps,2) (1:2*F3,1) .*Ramp_In'; (1:24Fs,2).*Ramp_In' soalmaaiiaems i cm j Alexandria University — Dept. of Elec. Eng. Third Year - Communications >>% Fading Out se¥(end-2*Fe+l:end,1)= Y(end-2*Fs+2:end,1) .*Ramp_Out'; ¥(end-2*¥s+1:end,2) .*Ramp_Out'; >>¥(end-2*Fs+1:end,2 sound (Y, Fa); >owavurite(¥,Fs,'File2'); Assignment 3 —> G@ncelied 1- Itis required to understand the distribution of sample values in the voice signal in file A3Q1,wav and in its first two derivatives. Generate the three histograms and describe the distributions, 2- The sound signal in the wave file A3Q2.wav has a defect. The right microphone channel was not connected properly during recording, so the sound appears from the left channel only. It is required to correct this problem by copying the contents of the left channel to the right channel. Produce the corrected wave file, (It might be better to use headphones in hearing the wave files for this problem.) S- It is required to generate a sound signal that could be used in testing an audio amplifier. The total duration of the file should be 42 seconds. The sound signal must consist of 14 segments. Each segment must be a sinusoid of duration 3 seconds with amplitude 0.5. The frequencies of the Segments should be 50 Hz, 100 Hz, 200 Hz, 400 Hz, 1 kHz, 2 kHz, 4 kHz, 6 kHz, 8 kHz, 10 kHz, 12 kHz, 14 kHz, 16 kHz, and 48 kHz. Generate a wave file that could be burnt on an audio CD and used in testing the amplifier. 4- Generate a siren signal that consists of a chopped double tone. Each cycle of the siren should Consist of 0.5 seconds of silence followed by 0.5 seconds of tone. The tone should be a sum of two sinusoids with frequencies 600 Hz and 1400 Hz. The amplitudes of the sinusoids must be equal to 0.25. The whole signal should comprise of 12 siren cycles. Generate this siren signal with a sampie rate of 22,050 Hz, and then save it in a wave file. 5- Write down a program that takes a vector of numbers representing the digits of a telephone. number, and returns a DTMF dialing tone signal corresponding to this number. Use a sample rate Of 20 kHz. Let the duration of the tone corresponding to any digit be 0.2 seconds, and the duration of the silence between two successive tones be 0.05 seconds. Let the amplitudes of all the single tones (which are added together to give the dual tone signals) be 0.15. The program should play the dialing signal. eee | Alexandria University ~ Dept. of Elec. Eng. Third Year - Communications Signal Processing using Matlab Lesson 4 Signal Processing in Frequency Domain 4.1 Introduction In this lesson, we will explain how we transform a signal from time domain to frequency domain and vice versa, and how to carry out frequency domain operations. We will start by studying the FFT and IFFT functions, and the auxiliary FFT shift function. We will then consider two examples of operations in the frequency domain. 4.2 FFT and FFT-Related Functions The Fast Fourier Transform (FFT) is a fast algorithm for computing the discrete Fourier transform of a discrete time limited signal. The following syntax shows how the FFT function is used. >>X: tx); In this instruction, x is the time domain signal, and X is its corresponding FFT result. The length of Xis the same as the length of x. There are several steps that are typically carried out after fit is invoked. The first typical step is invoking ‘fftshift’, a function that divides X into two halves, and swaps the position of the first and second half. This operation is usually required because the zero frequency location in the vector returned by FFT is not in the middle as we require, but at the start of the vector. After invoking the “fftshift’ function, the DC component is brought to the middle of the vector. This makes it easier in many cases to visualize the positive and negative frequency halves. The following two figures illustrate the effect uf ‘ftshift. The spectrum in figure 4.1 (a) was generated without invoking fishif. The spectrum in figure 4.1 (b) was generated using fftshift. 00 600 400 400 4 200 200 | 0 oO 200 Aco 00, 600 1000 600-300-100 100 300 600 @) ©) Figure 4.1 Note that the spectra plotted in figure 4.1 are for a cosine wave of frequency 50 Hz. The second plot is more illustrative of the positive and negative frequency halves. We also invoke the ‘abs’ (absolute) function to return the magnitude or the ‘angle’ function to return the phase of each element in the complex spectrum X as shown below. >>Xmagnitudesabs (X) ; >>Xphase=angle (X) The instructions we have written so far are responsible for generating the data for the y- coordinates of the plot points. Now, we wish to generate the data for the frequency-coordinates of the plot points. The vector of frequency coordinates is called the “trequency base vector’. It may be generated using the following instruction: >>Pvec=Linspace (-£s/2,£8/2,Ns): | Alexandria University — Dept. of Elec. Eng. Third Year - Communications *__In the previous instruction, fs Is the sample rate of the signal, and Ns is its number of samples. To sum up, let us consider the following example, in which we plot the magnitude and phase ‘spectra of a signal comprising of the sum of two sinusoids. Jinspace (0,5, 1000) ; Stcos(60*pitt)+0.3*sin(80*pirt) ; Etshift (££t (y)); >>Ymag=abs (¥); Yphasesangle (¥) ; >>Pvecelinspace(-100,100, 1000) ; pefigure; plot (Fvec, Ymag); titlu (‘Magnitude spectrum’) ; >>figure; plot (Fvec, Yphase); title(‘Phase Spectrum’) ; The lat FFT-related function we will consider is the inverse FFT (ifft) function. poxei FEE OO) ; 4.3 Examples on Frequency-Domain Operations |n this section, we will consider two examples of frequency-domain operations: convolution, and filtering. We will carry out convolution of two signals by multiplying their FFT results. We will carry out filtering by zeroing the frequency components of unwanted coefficients. 4.3.1 Convolution by Multiplication in Frequency Domain |n the following code, we want to convolute two time domain signals x1 and x2. We do so by first taking their FFT to get X1 and X2, multiplying them together, and then taking the IFFT of the product. zeros(1,20) ones(1, [zeros (1,20) linspace(o ££t (x2); poxl zeros (1,20)]; >ox2: poX1sfft (x1); x >oYeXL.*+X2; >eysreal (ifft (¥)); ‘The reason we have invoked ‘real’ in the last instruction is to get rid of residual imaginary values of infinitesimal magnitude. These values arise from the computational resolution of Matlab, and will typically be in the order of 10" or smaller. 4.3.2 Filtering In the following code, we want to filter a sample of sound of duration 2 seconds using a bandstop filter that suppresses frequencies in the range from 1 kHz to 2 kHz. The sample rate of the sound signal is known to be 22,05C initially, but is converted to 10,000 Hz. Thus, the number of ‘Samples in the sound wave thus becomes 2x10,000 = 20,000 samples. The frequency base vector is thus 20,000 points iong with a start value of -6,000 and an end value of 5,000. The points corresponding to the frequencies from -2 kHz to -1 kHz and from 1 kHz to 2kHz are the points number 6001 through 8000 and 72,001 through 14,000 (using ratio and proportion). In the following code, we will insert zeros in these locations in the FFT results, ‘Alexandria University — Dept. of Elec. Eng. Third Year - Communications Assignment 4 ——> Canceled You are required to design a new audio codec which produces an audio file format with a new extension called ‘.axt’. The AXF compressor takes the input sound wave and resamples it at 20 kHz. The samples in the wave are then split into frames of 20,000 samples each. The FFT frame length you will use is also 20,000. If the number of samples in the sound wave is not an integer number of frames, you should use zero padding to round it to the nearest higher integer number of frames. The sound frames are applied to a BPF that passes only those frequency coefficients corresponding to the frequency band from 400 Hz to 3,400 Hz. The frequency coefficients passed are then rounded so that they may be represented using an 8-bit signed binary representation with 6 bits before the binary point and 2 bits after the point for each of its magnitude and phase components. Each magnitude coefficient is preceded by an indication bit. If the magnitude of the binary coded sample value is greater than (000010.00),, the indication bit is set to 0 and attached before the 8 bits of the magnitude coefficient. The 8 bits of the phase coefficient follow the ‘magnitude coefiicient in this case. If the magnitude of the binary coded sample value is less than or equal to (000010.00},, the indication bit is set to 1. The indication bit is attached in the stream, but the 16 bits of the magnitude and phase coefficients are discarded. Write down a function M-fle that compresses a sound vector into an AXF file, and another function that takes the name of an AXF file and returns the sound signal once again. At the beginning of the file, you should write a header that contains information about the duration of the sound file, the number of frames, and the amount of zero padding you have Used so as to be able to oliminate those padded zeros at the receiver. lS ti ~ Dept. of Elec. Eng. Third Year - Communications Matlab Instructions for Lesson 4 z napace (0, 5/1000) fal Yy=0.5*cos (60*pi*t) +0.3*sin(@0*pi*t); ia Ae! 3 Y¥=fftshift (fft (y)); =< al 4 [ ¥mag=ebs(¥); ee S| ¥phase=angle(¥); - | Fveesiinepace(-100,100, 1000); 7 igure; plot (vec, Ynag); title( Wagaitude Spectrum’) B figure; plot (Fvec, Yphase); title(‘Phase Spect=um’ ); 9 eal (ifft(¥)); © ° m m e }oon ect s 10 X1=[zeros(1,20) ones(1,40) zeros(1,20)]; ii zaros(1,20) linspace (0,2,20) linspace(i,0,20) 2erea(1,20)17 Et (ocd); H2=EEt (x2); i5 =ewavread( Filel’); * 16 ‘esample(y, 10000, 22050 Sere Fig Oe OEE, 17 ( 0000) ; + ‘i , 18 ‘ftsh: (fit (y)): > if 1 rae Le ¥([6002:8000 1200: 20 | y2ereal (iffe(¥)): x = z 21 [sound (y2,10000)5 Z = ¢ 2 ° m f a e a t s

You might also like