adsp matlab report file

17
Advanced Digital Signal Processing MATLAB Assignment - Manish Kumar - U101112FEC186 - Batch - S1

Upload: mekusaha

Post on 10-Nov-2015

36 views

Category:

Documents


5 download

DESCRIPTION

Advanced Digital Signal Processesing

TRANSCRIPT

  • Advanced Digital Signal Processing

    MATLAB Assignment

    - Manish Kumar

    - U101112FEC186

    - Batch - S1

  • 1

    .

    EXPERIMENT 1

    Downsampling and Upsampling

    Theory:

    Upsampling : "Upsampling" is the process of inserting zero-valued samples between original

    samples to increase the sampling rate ("zero-stuffing"). Upsampling adds to the original

    signal undesired spectral images

    Downsampling: "Downsampling" is the process of reducing the sampling rate of the

    samples. Here, upsample(x,n) gives the upsampled signal by a factor of n.

    downsample(x,n) gives the downsampled signal by a factor of n.

    Matlab Code:

    t=1:40; fs=0.2; a=sin(2*pi*fs*t); subplot(311) stem(a); axis([0 20 -1 1]); grid on; title('ORIGINAL');

    %Upsampling ua=upsample(a,2); subplot(312) stem(ua); axis([0 20 -1 1]); grid on; title('UPSAMPLED BY 2');

  • 2

    .

    %Downsampling da=downsample(a,2); subplot(313) stem(da); axis([0 20 -1 1]); grid on; title('DOWNSAMPLED BY 2');

    Output:

    EXPERIMENT 2

    Interpolation and Decimation

    Theory:

    Interpolation : "Interpolation" is the process of upsampling followed by filtering. The

    filtering removes the undesired spectral images. The interpolation factor is simply the ratio

    of the output rate to the input rate.

  • 3

    .

    Decimation : It is filtering (to mitigate aliasing distortion) followed by downsampling.

    Here, interp(x,n) gives the interpolated signal by a factor of n.

    decimate(x,n) gives the decimated signal by a factor of n.

    Matlab Code:

    t= 0:(1/10000):((4/1000)-(1/10000)) m = cos(2*pi*1000*t) subplot(3,1,1) stem(m) title('Original signal');

    y1 = decimate(m,1); subplot(3,1,2) stem(y1) title('Decimation by 1'); axis([0 5 -1 1]);

    y2 = interp(m,4); subplot(3,1,3) stem(y2) title('Interpolation by 4');

    Output:

    EXPERIMENT 3

    Polyphase Decomposition

  • 4

    .

    Theory:

    Matlab Code:

    t= 0:1/100:1; x= sin(2*pi*t);

    t0=1:100; h=fir1(99,0.2,'low'); subplot(221); stem(t0,h);

    y=filter(h,1,x); subplot(222); stem(t,y);

    t1=0:0.1:1; yd= y(1:10:end); subplot(223); stem(t1,yd);

    hp= zeros(10,10); for i=1:10 hp(i,1:10)= h(i:10:end); end

    xp(1,1:11)=x(1:10:end); j=10; for i=2:10 xp(i,1:11)= [0 x(j:10:end)]; j=j-1; end

    yp=zeros(1,11); for i=1:10 yp= yp+ filter(hp(i,1:10),1,xp(i,1:11)); end

    subplot(224); stem(t1,yp);

  • 5

    .

    Output:

    EXPERIMENT 4

    SUB-BAND CODING- QMF

    Theory:

    QMF is used for splitting a signal into two or more subbands in the frequency domain, so that

    each subband signal can be processed in an independent manner and sufficient compression

    may be achieved.

    Eventually, at some point in the process, the subband signals are recombined so that the

    original signal is properly reconstructed

    Matlab Code:

    n= 10;

  • 6

    .

    p = fir1(19,1/2);

    for i= length(p) if(i > (length(p)/2)+1) p(i+1)= p(i); if(i == (length(p)/2)+1) p(i) = 0.28; else p(i)=p(i); end end end p=downsample(p,2); p=upsample(p,2); Z = roots(p); hZ= []; for i=1:length(Z) if abs(Z(i))>1 hZ = [hZ,Z(i)]; end end [h,d] = zp2tf(hZ',[],1); %coefficients of H0(z) h1 = wrev(h);

    for i = i:length(h1) if i ==1 || i==3 h1(i)= -h1(i); end end

    f0 = wrev(h); f1 = wrev(h1);

    [y, fs] = audioread('1.wav'); yl = filter(h,1,y(:,1)); yu = filter(h1,1,y(:,1)); yrl = filter(f0,1,yl); yru = filter(f1,1,yu); y1 = yrl + yru;

    fvtool(y(:,1),1,y1,1); fvtool(h,1,h1,1,p,1);

  • 7

    .

    Output:

    EXPERIMENT 5

    ADAPTIVE FILTERING USING LMS ALGORITHM

  • 8

    .

    Theory:

    Least mean squares (LMS) algorithms are a class of adaptibve filters used to mimic a desired

    filter by finding the filter coefficients that relate to producing the least mean squares of the

    error signal

    where

    x(n) is the input signal to a linear filter y(n) is the corresponding output signal d(n) is an additional input signal to the adaptive filter e(n) is the error signal that denotes the difference between d(n) and y(n).

    The LMS algorithm performs the following operations to update the coefficients of an adaptive FIR filter:

    1. Calculates the output signal y(n) from the FIR filter.

    Y(n = x(n)Tw(n)

    2. Calculates the error signal e(n) by using the following equation: e(n) = d(n)y(n)

    3. Updates the filter coefficients by using the following equation:

    w(n+1) = w(n) + *e(n)*x(n)

    where is the step size of the adaptive filter

    Matlab Code1:

    %ADAPTIVE FILTERING for signal estimation

    clc; clear all; close all; m=16; mu=0.05; %step size x=[];

  • 9

    .

    x(1)=0; w=zeros(16,1); u1=randn(1000,1); for n=2:1000 x(n)=0.5*(x(n-1)+u1(n)); %original signal end d=[]; d=x+cos(0.2.*pi.*(1:1000));

    ip =cos(0.2.*pi.*(1:1000)); %noise added to signal y=[]; e=[];

    for i=17:1000 y(i)=w'*(ip(i:-1:(i-m+1)))'; e(i)= d(i)-y(i); %error between i/p and estimated signal w=w+(mu*(ip(i:-1:(i-m+1)))'*e(i)); %update filter

    coefficients end

    error=zeros(1000,1); error(1)=0; for i=2:1000 error(i)=error(i-1)+(abs(x(i)-e(i))^2); %energy of error

    minimizes as samples increase end

    fvtool(d,1,y,1); fvtool(error,1);

    Output:

  • 10

    .

    Matlab Code 2: clc; clear all; close all; m=16; mu=0.05; %step size x=[]; x(1)=0; w=zeros(16,1); u1=randn(1000,1); for n=2:1000 x(n)=0.5*(x(n-1)+u1(n)); %original signal end

    h=fir1(15,0.2); d=filter(h,1,x);

    for i=1:20 %20 iterations for j=17:1000 y(j)=w'*(x(j:-1:(j-m+1)))'; e(j)= d(j)-y(j); %error between i/p and estimated signal w=w+(mu*(x(j:-1:(j-m+1)))'*e(j)); %update filter

    coefficients end end fvtool(h,1,w,1);

  • 11

    .

    Output:

    EXPERIMENT 6

    Linear and Circular Convolution

    Theory: One dimensional circular discrete convolution is given by :

    Matlab Code:

    x = [1 2 3 4 5]; subplot 221 stem(x); title('x'); axis([0 6 0 10]); y = [3 6 8]; subplot 222 stem(y); title('y'); axis([0 6 0 10]);

  • 12

    .

    c1 = conv(x,y); subplot 223 stem(c1); title('linear convolution'); axis([0 10 0 70]);

    c2 = cconv(x,y,70); subplot 224 stem(c2); title('circular convolution'); axis([0 20 0 70]);

    Output :

    EXPERIMENT 7

    MFCC

    Theory:

    Mel Frequency Cepstral Coefficents (MFCCs) are a feature widely used in automatic speech

    and speaker recognition.

    The steps to calculate MFCCs are as follows:

    1. Frame the signal into short frames.

    2. For each frame calculate the periodogram estimate of the power spectrum. 3. Apply the mel filterbank to the power spectra, sum the energy in each filter.

    4. Take the logarithm of all filterbank energies.

    5. Take the DCT of the log filterbank energies.

    6. Keep DCT coefficients 2-13, discard the rest.

    Matlab Code:

    clear all;

  • 13

    .

    close all;

    [z,Fs] = audioread('1.wav');

    %10ms of sample m = round(0.01*Fs);

    f = fft(z); log_mag = log(abs(f)); c = ifft(log_mag); glottal = [zeros(1,m) c(m:end-m)' zeros(1,m)]; vocal = [c(1:m)',zeros(1,length(c)-(2*m)),c(end-m:end)']; f_glottal = fft(glottal); f_vocal = fft(vocal);

    figure('Name','Cepstrum 1') subplot(421), plot(z), title('Audio sample of 10ms'); subplot(422), plot(abs(fftshift(f))), title('FFT of sample of 10ms'); subplot(423), plot(log_mag), title('Log magnitude of sample of 10ms'); subplot(424), plot(c), title('Cepstrum of sample of 10ms'); subplot(425), plot(glottal), title('Glottal Value'); subplot(426), plot(vocal), title('Vocal coefficients'); subplot(427), plot(abs(fftshift(f_glottal))), title('FFT of Glottal

    Value'); subplot(428), plot(abs(fftshift(f_vocal))), title('FFT of Vocal

    coefficients');

    Output:

  • 14

    .

    EXPERIMENT 8

    WAVELET TRANSFORM

    Theory:

    The discrete wavelet transform (DWT) represents a 1-D signal s(t) in terms of shifted versions of a

    lowpass scaling function (t) and shifted and dilated versions of a prototype bandpass wavelet

    function (t).

    Haar Wavelets

    For an input represented by a list of numbers, the Haar wavelet transform may be considered to pair

    up input values, storing the difference and passing the sum. This process is repeated recursively, pairing

    up the sums to provide the next scale, which leads to differences and a final sum.

    Matlab Code 1:

    close all; clear all; clc;

    [ak,bk] = wfilters('db2');

    A = [bk(1) 0 0 0;bk(3) bk(2) bk(1) 0; 0 bk(4) bk(3) bk(2); 0 0 0 bk(1)]; A = 2*A; [v,b] = eigs(A); v(1:4,1)= v(1:4,1)/sum(v(1:4,1)); % lambda ~ 1 N = 4; phi = Scale(v(:,1),bk);

    for i = 1:9 subplot(3,3,i) phi = Scale(phi,bk); plot(phi); end

    %We get round off errors because we are not multiplying/taking lambda value

    = 1 %Try by putting ak also in matrix A

    function [ phi ] = Scale( phi,bk ) phi = upsample(phi,2); bk1 = [bk,zeros(1,length(phi)-length(bk))]; % phi = [phi,zeros(1,length(phi)-length(bk))]; for i = 2:2:length(phi) for k = 1:2*i-1 if(i

  • 15

    .

    Output:

    Matlab Code 2:

    % wavelet transform

    x= randn(1,1000); y= zeros(1,1000); for i= 500:550 y(i)= 10.*cos(0.9*pi*i); end x=x+y; subplot(221) plot(x); title('x'); [ca,cd]= dwt(x,'haar'); [ca,cd]= dwt(ca,'haar'); subplot(222) plot(ca); title('ca'); subplot(223) plot(cd); title('cd');

  • 16

    .

    Output: