Matlab Audio SNR MSE

15
Section 1-C: Design and Implementation of Lowpass FIR Filters. 1. Referring to the plots in Section 1-B, the passband edge frequency, fp and the stopband edge frequency, fs are determined as follows: fp = 1900Hz fs = 2000Hz Therefore, the cutoff frequency, fc = (fp + fs)/2 = (1900 + 2000)/2 = 1950Hz Given the sampling frequency ft >= 8000, The normalized cutoff frequency, Wn = (fp + fs)/ft = (1900 + 2000)/8000 = 0.4875 2. To achieve a passband gain of |Gp| < 3 dB and a stopband attenuation of |Gs| > 20 dB, the minimum N (length of the lowpass filter) was found to be N = 142 . 3. Using the fir1 MATLAB function, the designed filter frequency responses (magnitude and phase) are plotted. Below is the MATLAB code used to design the FIR filter and to plot the designed filter's frequency response: MATLAB code for designing the FIR filter using fir1 MATLAB function and to plot it's frequency response, B=fir1(142, 0.4875); freqz(B,1,142,8000); %[noisy1,freq]=wavread('noisy1.wav'); %[orig1,freq]=wavread('orig1.wav'); %y=filter(B,1,noisy1);

Transcript of Matlab Audio SNR MSE

Page 1: Matlab Audio SNR MSE

Section 1-C: Design and Implementation of Lowpass FIR Filters.

1.  Referring to the plots in Section 1-B, the passband edge frequency, fp and the stopband edge      frequency, fs are determined as follows:            fp = 1900Hz            fs = 2000Hz

    Therefore, the cutoff frequency, fc = (fp + fs)/2                                                         = (1900 + 2000)/2                                                         = 1950Hz     Given the sampling frequency ft >= 8000,     The normalized cutoff frequency, Wn = (fp + fs)/ft                                                              = (1900 + 2000)/8000                                                              = 0.4875

2.  To achieve a passband gain of |Gp| < 3 dB and a stopband attenuation of |Gs| > 20 dB, the      minimum N (length of the lowpass filter) was found to be N = 142.

3.  Using the fir1 MATLAB function, the designed filter frequency responses (magnitude and      phase) are plotted. Below is the MATLAB code used to design the FIR filter and to plot the      designed filter's frequency response:

     MATLAB code for designing the FIR filter using fir1 MATLAB function and to plot it's      frequency response,

                      B=fir1(142, 0.4875);             freqz(B,1,142,8000);           %[noisy1,freq]=wavread('noisy1.wav');             %[orig1,freq]=wavread('orig1.wav');             %y=filter(B,1,noisy1);

            %wavwrite(y,'filtered_fir.wav');             %soundsc(y,8000);

     The designed FIR filter's frequency response (both magnitude and phase):

Page 2: Matlab Audio SNR MSE

     The filter coefficients are saved in a Notepad text file: FIR Filter Coefficients

     The impulse response of the filter is plotted using the following MATLAB code:

        B=fir1(142, 0.475);         stem(B);         title('Impulse Response of Designed FIR Filter');         xlabel('Index, n');         ylabel('Magnitude');         grid;

      %[noisy1,freq]=wavread('noisy1.wav');         %[orig1,freq]=wavread('orig1.wav');

        %y=filter(B,1,noisy1);         %wavwrite(y,'filtered_fir.wav');         %soundsc(y,8000);

Page 3: Matlab Audio SNR MSE

Please Note: The above graph is not very clear after being exported into jpeg format. For                       verification purposes, use the M-file provided above.

4.  The noise is filtered out from the signal using the following MATLAB code:

        B=fir1(142, 0.4875);         freqz(B,1,142,8000);         [noisy1,freq]=wavread('noisy1.wav');         [orig1,freq]=wavread('orig1.wav');         y=filter(B,1,noisy1);

        wavwrite(y,'filtered_fir.wav');         soundsc(y,8000);

     *Here is the filtered signal in ".wav" format for evaluation purposes: filtered_fir.wav

5.  MATLAB codes for finding MSE and SNR:

Page 4: Matlab Audio SNR MSE

      MSE=(sum((y-orig1).^2)/65536)       SNR=10*log10((sum(orig1.^2))/65536/MSE)

     For filtered signal,      Mean Squared Error, MSE =  0.0549

     Signal to Noise Ratio, SNR = -5.1928

     For noisy signal,      Mean Squared Error, MSE =  0.0573

     Signal to Noise Ratio, SNR = -5.3796

     MATLAB code for plotting the original and the filtered signal on the same figure for time      domain:

      %Read sound files         %'signal' is the vector holding the original samples &         %'freq' refers to the sampling frequency

        [signal, freq]= wavread('orig1.wav');         n = length(signal);         %length of the original signal

      %plot original signal in time domain         subplot(211);         plot((1:n)/freq,signal);         title('Original Signal in Time Domain');         xlabel('Second(s)');         grid;

        [signal, freq]= wavread('filtered_fir.wav');         n = length(signal);           %length of the original signal

      %plot filtered signal in time domain         subplot(212);         plot((1:n)/freq,signal);         title('Filtered Signal in Time Domain');         xlabel('Second(s)');         grid;

Page 5: Matlab Audio SNR MSE

     MATLAB code for plotting the original and the filtered signal on the same figure forfrequency      domain (Magnitude):

        [signal, freq]= wavread('orig1.wav');         n = length(signal);

      %plot original signal in frequency domain.         subplot(211);         signal0=signal - mean(signal);         fsignal = fft(signal0);     %remove DC component             %Use fast fourier Transform to             %transform the original signal             %into frequency domain.         plot((1:n/2)/n*freq,abs(fsignal(1:n/2)));         title('Original Signal in Frequency Domain');         xlabel('Hz');

Page 6: Matlab Audio SNR MSE

        ylabel('Magnitude');         grid;

        [signal, freq]= wavread('filtered_fir.wav');         n = length(signal);

      %plot filtered signal in frequency domain.         subplot(212);         signal0=signal - mean(signal);         fsignal = fft(signal0);     %remove DC component             %Use fast fourier Transform to             %transform the original signal             %into frequency domain.         plot((1:n/2)/n*freq,abs(fsignal(1:n/2)));         title('Filtered Signal in Frequency Domain');         xlabel('Hz');         ylabel('Magnitude');         grid;

Page 7: Matlab Audio SNR MSE

     MATLAB code for plotting the original and the filtered signal on the same figure for frequency      domain (Phase):

        [signal, freq]= wavread('orig1.wav');         n = length(signal);                 %length of the original signal

        subplot(211);         plot((1:n/2)/n*freq,unwrap(angle(fsignal(1:n/2))));       %unwrap(P) corrects the radian phase angles in array P by adding         %multiples of ±2*pi when absolute jumps between consecutive array         %elements are greater than pi radians.         %If P is a matrix, unwrap operates columnwise. If P is a         %multidimensional array, unwrap operates on the first         %non-singleton dimension.         %P can be a scalar, vector, matrix, or N-D array.         title('Original Signal in Frequency Domain');         xlabel('Hz');         ylabel('Phase');         grid;

        [signal, freq]= wavread('filtered_fir.wav');         n = length(signal);           %length of the original signal

        subplot(212);         plot((1:n/2)/n*freq,unwrap(angle(fsignal(1:n/2))));       %unwrap(P) corrects the radian phase angles in array P by adding         %multiples of ±2*pi when absolute jumps between consecutive array         %elements are greater than pi radians.         %If P is a matrix, unwrap operates columnwise. If P is a         %multidimensional array, unwrap operates on the first         %non-singleton dimension.         %P can be a scalar, vector, matrix, or N-D array.         title('Filtered Signal in Frequency Domain');         xlabel('Hz');         ylabel('Phase');         grid;

Page 8: Matlab Audio SNR MSE

     The filtered signal sound almost as if it was the original signal. Upon hearing the filtered signal, no      major difference could be distinguished.

6.  MATLAB program written to quantize the coefficients of the designed filter into:         (i)   4-bit.         (ii)  8-bit.         (iii) 12-bit.         (iv) 16-bit.

     After measuring the frequency responses of the filters with quantized coefficients, the smallest      number of bits required for the FIR filter that can satisfy the specifications (without the need to      redesign the filter) was found to be 12-bits. Here is the MATLAB code to quantize the      coefficients of the desined filter into 12-bits:

Page 9: Matlab Audio SNR MSE

        b=fir1(142,0.4875);         N=12;         B=(b*(2^(N-1))+0.5)/2^(N-1);         freqz(B,1,142,8000);         [noisy1,freq]=wavread('noisy1.wav');         [orig1,freq]=wavread('orig1.wav');         y=filter(B,1,noisy1);         wavwrite(y,'fir_12bits.wav');         soundsc(y,8000);

     Frequency Response of designed filter (with its coefficients represented using 12-bits):

7.  MATLAB codes for finding MSE and SNR:

      MSE=(sum((y-orig1).^2)/65536)       SNR=10*log10((sum(orig1.^2))/65536/MSE)

Page 10: Matlab Audio SNR MSE

     For filtered signal (quantized),      Mean Squared Error, MSE =  0.0578

     Signal to Noise Ratio, SNR = -5.4187

     Plotting both the original signal and filtered signal (12-bits) on the same figure (using the      MATLAB code given in the lab sheet) for:      (a) Time domain.      (b) Frequency domain (Magnitude).      (c) Frequency domain (Phase).

     *Here is the filtered signal in ".wav" format for evaluation purposes: fir_12bits.wav      *The filter coefficients (represented by 12-bits) are saved in a Notepad text file: filtered.txt

Page 11: Matlab Audio SNR MSE

 

Page 12: Matlab Audio SNR MSE

     Once again, the filtered version sound similar to the original signal. We cannot distinguish any differences based on hearing alone. However, we can distinguish any differences between the two signals by comparing the signals in time and frequency domain. Yet, it is also quite impossible to differentiate between the original signal and the filtered signal in the time domain. Through the frequency response, we observed that the noises (signals above 3400Hz and the two spikes) has been eliminated, hence the magnitude response of the filtered signal showed that attenuation occurred.

     It is also observed that the magnitude of the filtered signal is on average, smaller than the original signal. This is why the SNR found is negative where actually it should be the opposite showing that the filtered signal is very close to the original signal.