University of Victoria Faculty of Electrical Engineering ELEC 499 – PROJECT Automatic Stereo Equalization Project Supervisor: Michael McGuire Report Authors: Yang Gao - V00171156 Qianqian Wang - 0421609 Nan Xiao - V00153730 Dong Zhang - V00202855 Ref: ELEC499-Gr12 Date: April 3, 2009 Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization Abstract This paper presents a low-complexity implementation of an audio playback enhancement method for sound quality improvement where the input audio signal is divided into a number of sub-bands that are individually weighted in frequency domain according to the weight (gain) calculation algorithm in each sub-band at every certain frequency. Basically, the weight is affected by the path loss of propagation throughout the diverse channels, limitations of hardware and calculated by the amplitude of playback sampled signal over the amplitude of original sampled signal. Instead of focusing on suppression the noise, the method is focusing on playback sound enhancement, and it has been proven to be advantageous when implemented in Matlab since it offers low complexity, low delay and low distortion. Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization Table of Contents 1 Introduction ............................................................................................................... 1 2 Problem Formulation and Method ............................................................................ 2 2.1 Mathematical Description ................................................................................... 3 2.1.1 Direct-Gain Approach ......................................................................................... 3 2.1.2 Filter-Bank Approach .......................................................................................... 5 2.1.2.1 Reducing the order of FIR filter for cut-off frequency below 2 kHz .... 6 3 Evaluation and Result ................................................................................................ 7 3.1 Filter Bank .......................................................................................................... 7 3.2 Simulation Result................................................................................................ 8 4 Conclusion ................................................................................................................. 9 5 References ............................................................................................................... 10 6 Appendices .............................................................................................................. 10 Table of Figures Figure 1 Block Diagram of Playback Equalizer System .................................................. 2 Figure 2 Direct Gain Approach to Equalizer Design ........................................................ 4 Figure 3 Filter Bank Approach to Equalizer Design ........................................................ 5 Figure 4 Wavelet Approach to Equalizer Design ............................................................. 6 Figure 5 Magnitude Response of 21 FIR filters with a cut-off frequency below 2.205 kHz ...................................................................................................................... 7 Figure 6 Magnitude Response of 6 FIR filters with a cut-off frequency above 2.205 kHz ...................................................................................................................... 8 Figure 7 Simulation Result in Matlab ............................................................................... 9 Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization 1. Introduction An implementation of fairly complex algorithms on digital signal processors (DSP) is nowadays possible thanks to the rapid development in accessible computational power during recent decades. Today, it is possible to exploit this computational power in order to implement complex algorithms. Human communication of today often occurs via various communication links. Even in very noisy environment, it is possible to communicate reliably. However, surrounding noise will degrade music quality and intelligibility, forcing both the far-end and near-end user to strain their hearing. An ordinary scenario where low quality audio is harmless but nevertheless distorting, is a live concert, which is held in varying noisy environment such as in various musical instruments or in a crowd. Altogether, this urges for noise reduction methods. However, complete noise reduction in the reality is so ideal and impractical that we have to give in the compromise between sound quality and other factors, such as the addictive noise and the hardware limitation. In this paper, a general, stand-alone, frequency-domain method for audio playback quality improvement is implemented in Matlab. The method is based on the Fourier Transform and is a nonlinear, yet straightforward way of reducing the influence of both unwanted internal and external factors acoustically added to a signal rather than removing them. The goal of this project is to develop a stereo system to automatically equalize itself to optimize the audio playback quality. This will be accomplished with an adaptive equalizer being placed before the speakers in the stereo system and microphones placed throughout the room where the stereo is located. The equalizer will compare the sound being heard at the microphones with the signal that is being sent to the speaker and calculate the required equalization settings to optimize the sound quality. A block diagram for the equalizer system is shown below. -1- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization 2. Problem Formulation and Method In a typical situation where an audio signal is distorted by noise, the noise, 𝑤(𝑛), is acoustically added to the audio signal, 𝑠(𝑛), by a microphone. The goal is to calculate the difference caused by the internal loss of speaker or microphone, and the channel loss through propagation using a comparison method of record signal power and original signal power. The basic idea behind the method described in this paper, is that a audio signal corrupted by various factors can be divided into a number of sub-bands and that each of these sub-bands can be individually and adaptively weighted according to a power comparison in that particular sub-band signal. A higher or lower sub-band power in the recorded signal indicates that the sub-band recorded signal content is added or subtracted by the channel. Hence, this sub-band of original signal through the equalizer should be suppressed or boosted respectively. For example, if the signal received by a microphone has smaller magnitude than original signal at the very frequency range, the equalizer system will bring them up before playing at speakers, and vice versa. To achieve this signal compensation process, a discrete-time power is calculated in each sub-band of both original and recorded signal. A noise constant is calculated simultaneously. Using the quotient of these quantities, a gain function can be achieved and thus weights the sub-band signal -2- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization directly or adaptively at that particular frequency range. For example, if only noise is present in the signal, in other words, the original signal is relatively small than noise signal, the original signal power plus noise power and recorded signal power will be in the same order of magnitude, hence the quotient of these two quantities will be unity. However, if audio signal is present, the original signal power will increase but the noise power will remain approximately unchanged. Hence, the quotient will become larger than unity, amplifying the signal in the sub-band. It is also possible that a sub-band with a quite low power due to the hardware limitation, may contribute to a big gain, and thus the gains weighted directly or adaptively to each sub-band should be constrained appropriately, avoiding the vocal to reach the break limit. Note that the method is focusing on playback audio quality improvement rather than noise suppression. It is assumed that the experiment is held in an approximately unchanged environment, and thus the noise power measured in the particular environment should result in a constant. Moreover, the recording process should proceed in the same environment. 2.1 Mathematical Description As mentioned early, the environmental transfer function (gain) is calculated by recorded signal power over the sum of original signal power and noise power constant; the equalizer coefficients is eventually equal to the inverse of the previous quotient. Later, these equalizer coefficients will be applied to the original signal directly or adaptively. 2.1.1 Direct-Gain Approach Suppose we have an acoustical noise denoted 𝑤(𝑛) and an audio signal denoted 𝑠(𝑛). The noise corrupted audio signal 𝑥(𝑛) can then be written as 𝑥(𝑛) = 𝑠(𝑛) + 𝑤(𝑛); the recorded audio signal is denoted by 𝑥’(𝑛). By applying Fourier Transform to both original and recorded audio signal and dividing them into K blocks with equal lengths, each block of signal 𝑥(𝑛) and 𝑥’(𝑛) is denoted by 𝑋𝑘 (𝑛) and 𝑋𝑘 ′(𝑛) -3- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization respectively, and thus the equalizer coefficients can by described as: 𝑊𝑘 = 𝑝𝑜𝑤𝑒𝑟{𝑋𝑘 (𝑛)⁄𝑋𝑘 ′(𝑛)} For discrete-time signals, we define power as energy per sample 1 𝑃𝑑 = (𝑖𝑛𝑑𝑒𝑥2 − 𝑖𝑛𝑑𝑒𝑥1 + 1) 𝑖𝑛𝑑𝑒𝑥2 ∑ (|𝑥(𝑛)|)2 𝑛=𝑖𝑛𝑑𝑒𝑥1 where index1 and index2 are the sample index starting or ending with respectively. Since the calculation of Wi involves a division, care must be taken to ensure that the quotient does not become excessively large due to a small recorded audio power. For example, in a situation with a small recorded audio power, Wi will become very large if no limit is imposed on this function, resulting in unacceptable high vocal sound amplification to reach the break limit. A limiter can be imposed on Wi as follows: 𝑊𝑖 = { 𝑊𝑖 𝐶 𝑖𝑓 𝑊𝑖 < 𝐶 𝑖𝑓 𝑊𝑖 ≥ 𝐶 where C is some positive constant. Then, the original audio signal will be weighted directly with blocks, and result in the equalized output signal. This process can be illustrated in the block diagram as below. The output of this system, denoted by 𝑥𝑛𝑒𝑤 , is described as: -4- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization 𝐾 𝑥𝑛𝑒𝑤 = ∑ 𝑥𝑖 (𝑛) × 𝑊𝑖 𝑖=1 where K is the number of blocks that are divided properly. Since weighting gains directly to the original audio signal acts as ideal filters, there will be an abrupt change at each block interface. Therefore, the continuity of the audio will be broken, and as a result, one can hear the “clicks” clearly at each block interface. 2.1.2 Filter-Bank Approach In order to overcome the effect of “clicks”, another approach is proposed: instead of applying the gains directly, it is going to firstly deploy a bank of K FIR filters, ℎ𝑘 (𝑛), filtering the input signal 𝑥(𝑛) , and then feed the weight in each sub-band correspondingly, merge all the piecewise audio signal together in the end. This process can be illustrated in the block diagram as below. The output of this system, denoted by 𝑥𝑛𝑒𝑤 , is described as: 𝐾 𝑥𝑛𝑒𝑤 = ∑ 𝑥𝑖 (𝑛) ∗ ℎ𝑖 (𝑛) × 𝑊𝑖 𝑖=1 where Wi is the weight that calculated in the same way described in Direct-Gain -5- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization method, * is a convolution operator, and hi (n) is the impulse response of each FIR filter. Since the audio is sampled at 44.1 kHz, at very small frequency range, such as [30Hz, 50Hz], the order of the FIR filter at this range is so high, and for example, the order is 2000, to meet the good filtering performance, resulting in a large number of computations, as running the algorithm. 2.1.2.1 Reducing the order of FIR filter for cut-off frequency below 2 kHz In order to reduce the order of FIR filter, the sampling rate has to be reduced as well. This can be achieved by low-passing the input audio signal at 2.205 kHz, down-sampling by 10, and deploying a bank of several FIR filter to apply the equalization as described in previous section, up-sampling by 10 to recover the sampling rate; in the meanwhile, the input audio signal above 2.205 kHz also passes through the equalizer using the filter bank approach. In the end, merge the two piece of audio together. This process can be illustrated in the block diagram as below. In order to ensure the same delay from two pathways shown in above figure, it can be solved by using the same order for low-pass, high-pass filters and FIR filters in the equalizer algorithm. Therefore, the number of computations in terms of CPU time is greatly reduced -6- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization 3. Evaluation and Result A general mathematical description of the method was given in section 2.1 where a number of sub-band dependent parameters were introduced. In this section, an evaluation is performed and verified in Matlab. All experimental evaluations have been performed on signals recorded on site at a sampling frequency of 44.1 kHz. 3.1 Filter Bank In section 2.1, a bank of bandpass filters was used to divide the input signal into K sub-bands. The FIR filters used in this paper are designed with the window method using Kaiser windows and the same order of 200. This method results in causal, symmetric impulse responses with linear phase and the same delay. The number of filters that are going to use below 2.205 kHz is 21, and the magnitude response of them is generated in Matlab as below. -7- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization Figure 5: Magnitude Response of 21 FIR filters with a cut-off frequency below 2.205 kHz The number of filters that are going to use above 2.205 kHz is 6, and the magnitude response of them is generated in Matlab as below. Figure 6: Magnitude Response of 6 FIR filters with a cut-off frequency above 2.205 kHz 3.2 Simulation Result In this paper, the focus has been on FIR based sub-band filters. The order of these filters has proven to be of some importance. As an example, using 21 sub-bands at low frequency range, the equalization capabilities increased noticeably when increasing the filter order from 64 to 200 taps. However, computational complexity limits our choice of filter order and frequency resolution to approximately 200 filter taps when using 21 sub-bands. In addition, the upper bounding of the gain function 𝑊𝑖 affects the resulting music distortion and should be kept within 5-10 dB. A larger amplification of the signal may result in piercing sounding output music. Note that the hardware limitation is such a crucial factor that it cannot be eliminated by no matter how one modifies the Matlab code desperately. For example, supposed there is a bass loss at speaker or microphone, thus the simulation result of equalization algorithm is -8- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization inconsistent with the desired expectations, no matter what solid work one contributes to one’s code. Therefore, with the very severe loss at certain frequency band, the original audio portion correspondingly remains unchanged, instead of equalizing it. Here is the simulation result generated by Matlab as below. Figure 7: Simulation Result in Matlab It is clearly shown that this equalizer does compensate the signal very well. For example, at the range of 20-30 Hz, the magnitude of the recorded music signal was brought down; nevertheless, the signal would be brought back up after feeding through the equalizer. For equalizer audio output, please visit project webpage at http://web.uvic.ca/~gy/Result2.html 4. Conclusion A playback audio signal enhancement algorithm of today must be flexible yet robust, easily implemented yet computationally efficient and, in addition, it must be versatile and applicable to many different noise situations. The method described in this paper has been implemented only in Matlab. It has been shown that the method fulfills all of these above-mentioned criteria: Thanks to the straightforward basic underlying idea, the method is flexible, robust and easily implemented. The filter bank structure used -9- Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization in this paper was an FIR filter bank but principally, any filter bank could be employed to obtain desired features and characteristics. In addition, experiments have shown that the method is easily configured and adapted to an unchanged environment; a real-time structure will be modified and tested in the future. 5. References Dr. Michael L McGuire Digital Signal Processing Group Department of Electrical and Computer Engineering University of Victoria 6. Appendices Matlab code: %-------------------------------------------------------------------------------------------------------------------------%MyMain.m %-------------------------------------------------------------------------------------------------------------------------clear all close all clc WcL = [0 20 25 31 40 50 63 80 100 125 160 200 250 315 400 500 630 800 1000 1250 1700];%cutoff frequencies LPMaxGain = 10*ones(1,22);%22 lowpass max gains WcH = [2205 4300 6000 8000 12000 15000];%cutoff frequencies HPMaxGain = 5*ones(1,22);%6 highpass max gains [Noise,FS,NBITS]=WAVREAD('Noise8.wav'); Noise = Noise(:,1); %load samples.mat; [Y,FS,Nbit] = wavread('Original8.wav'); Y = Y(:,1); [Y3,FS,Nbit] = wavread('Record8.wav'); Y3 = Y3(:,1); [Y4,FS,Nbit] = wavread('Recordeq8.wav'); Y4 = Y4(:,1); - 10 - Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization %zero padding to get equal length Lengths = [length(Y),length(Y3),length(Noise)]; Max_Length = max(Lengths); Max_Length = 10*ceil(Max_Length/10);%last digit change to 0 Y = [Y;zeros(Max_Length-length(Y),1)]; Y3 = [Y3;zeros(Max_Length-length(Y3),1)]; Noise = [Noise;zeros(Max_Length-length(Noise),1)]; %LPF Wc = 2205; [BL, AL] = fir1(200,2205*2/FS); %[HLP WLP] = freqz(B,A); %HPF Wc = 2205; [BH, AH] = fir1(200,2205*2/FS,'high'); YL = filter(BL,AL,Y); Y3L = filter(BL,AL,Y3); NoiseL = filter(BL,AL,Noise); YH = filter(BH,AH,Y); Y3H = filter(BH,AH,Y3); NoiseH = filter(BH,AH,Noise); YLD = downsample(YL,10); Y3LD = downsample(Y3L,10); NoiseLD = downsample(NoiseL,10); FSD = FS/10; SpeakerD = MyEqualizer(YLD,Y3LD,NoiseLD,FSD,WcL,LPMaxGain); Speaker1 = interp(SpeakerD,10); Speaker2 = MyEqualizer(YH,Y3H,NoiseH,FS,WcH,HPMaxGain); Speaker = Speaker1+Speaker2; SpeakerF = fft(Speaker); %--------------------------------------------------------%Plots %--------------------------------------------------------figure(1) subplot(3,1,1) MyPlot(Y,FS,[0 FS/2 0 10000]) subplot(3,1,2) - 11 - Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization MyPlot(YL,FS,[0 FS/2 0 10000]) subplot(3,1,3) MyPlot(YH,FS,[0 FS/2 0 10000]) figure(2) subplot(4,1,1) MyPlot(Y,FS,[0 2205 0 10000]) subplot(4,1,2) MyPlot(Y3,FS,[0 2205 0 10000]) subplot(4,1,3) MyPlot(Speaker,FS,[0 2205 0 10000]) subplot(4,1,4) MyPlot(Y4,FS,[0 2205 0 10000]) %-------------------------------------------------------------------------------------------------------------------------%MyEqualizer.m %--------------------------------------------------------------------------------------------------------------------- ----function [Speaker, H, MyFilter] = MyEqualizer(Y,Y3,Noise,FS,Wc,MaxGain) %--------------------------------------------------------%Constant definitions %--------------------------------------------------------N = 200; %order of the FIR filter wn=kaiser(N+1,0.5);%Kaiser window for i = 1:1:length(Wc) %mid points of cutoff frequencies if i ~= length(Wc) Wc_m(i) = (Wc(i)+Wc(i+1))/2; else Wc_m(i) = (Wc(i)+FS/2)/2; end end YF = fft(Y); YF3 = fft(Y3); NoiseF = fft(Noise); %--------------------------------------------------------%Filter Design and Equalization %--------------------------------------------------------for i = 1:1:length(Wc) if i ~= length(Wc) index1 = floor((length(Y)/FS)*Wc(i))+1; - 12 - Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization index2 = floor((length(Y)/FS)*Wc(i+1)); if Wc(i) ~=0 MyFilter(i,:) = fir1(N,[2*Wc(i)/FS,2*Wc(i+1)/FS],wn);%bandpass else MyFilter(i,:) = fir1(N,2*Wc(i+1)/FS,wn);%lowpass end else index1 = floor((length(Y)/FS)*Wc(i))+1; index2 = floor(length(Y)/2); MyFilter(i,:) = fir1(N,2*Wc(i)/FS,'high',wn);%highpass end TSignal_Pwr(i) = sum((abs(YF(index1:index2))).^2)/(index2-index1+1); RSignal_Pwr(i) = sum((abs(YF3(index1:index2))).^2)/(index2-index1+1); Noise_Pwr(i) = sum((abs(NoiseF(index1:index2))).^2)/(index2-index1+1); Noise_Amp(i) = sqrt(Noise_Pwr(i)); %SNR > 10 if (TSignal_Pwr(i) < 10*Noise_Pwr(i))||(RSignal_Pwr(i) < 10*Noise_Pwr(i)) H(i) = 1; else H(i) = sum(abs(YF3(index1:index2)))/(sum(abs(YF(index1:index2)))+(index2-index1)*Noise_Amp(i)) ;%Gain = Sr/(S1+N1) end H2(i) = 1/H(i); if H2(i)>MaxGain(i) %Gain < MaxGain H2(i) = MaxGain(i); end %fvtool(MyFilter(i,:),1); end H2 %Equalize original signal SpeakerF = 0; %Speaker = 0; for i = 1:1:length(H2) %figure(i) %asd = abs(fft(filter(MyFilter(i,:),1,Y))); %plot(f, asd(1:ceil(length(Y)/2))) %axis([0 22050 0 2000]) %Speaker = Speaker+filter(MyFilter(i,:),1,Y)*H2(i); SpeakerF = SpeakerF+fft(filter(MyFilter(i,:),1,Y))*H2(i); end Speaker = ifft(SpeakerF); - 13 - Ref : ELEC499-Gr12 Date: April 3, 2009 Automatic Stereo Equalization %--------------------------------------------------------------------------------------------------------------------- ----%MyPlot.m %-------------------------------------------------------------------------------------------------------------------------function MyPlot(Y,FS,Scale) YF = fft(Y); f = (0:FS/(length(Y)-1):FS/2)'; YFH = YF(1:ceil(length(Y)/2)); semilogy(f, abs(YFH)) axis(Scale) %-------------------------------------------------------------------------------------------------------------------------------------% MyPowerMatcher.m %-------------------------------------------------------------------------------------------------------------------------------------%This function was used to match volume of output music to the input for demo purposes. function [Y2Moded] = MyPowerMatcher(Y2, Y1) Avg_Pwr1 = sum((abs(Y1)).^2)/length(Y1); Avg_Pwr2 = sum((abs(Y2)).^2)/length(Y2); Y2Moded = sqrt(Avg_Pwr1/Avg_Pwr2)*Y2; - 14 -