Breaking

Be Strong Let's Try!

Saturday, 28 August 2021

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB

 What is the Objectives of this topic:-

The objectives of this topic are to learn the up sampling and down sampling of a signal while taking care of aliasing and to learn the interpolation using polyphase decomposition.


What is the Procedure:-

In this topic we are going to process audio signals by down sampling it again and again to a limit that aliasing starts to occur. In the second step we are going to interpolate the decimated signal to reconstruct the original signal. 

MATLAB Functions:-

To downsample a signal, you can use MATLAB function downsample. This function  

y = downsample(x,n) 

decreases the sample rate of x by keeping the first sample and then every nth sample after the first. If x is a matrix, the function treats each column as a separate sequence.

To upsample a signal, you can use MATLAB function upsample. This function  

y = upsample(x,n) 

You can also use decimate function to decimate the audio signal use which automatically applies the antialiasing filter before downsampling,

Description:-

y = decimate(x,r) reduces the sample rate of x by a factor r.

For upsampling you can use the function interp

Description:-

Interpolation increases the original sampling rate for a sequence to a higher rate. 

y = interp(x,r) increases the sampling rate of x by a factor of r.


Now we will try to understand this topic with some of the practical example using the MATLAB:

 

 

01: Mixing of two Signals

1.           Record an audio signal saying @ 44.1kHz:

Welcome to Digital Signal Processing Course.

and plot the signal in time and frequency domain with all axes correctly labeled. Note down the frequency spectrum occupied by your recorded voice. You can play the audio by using the MATLAB command sound. Note: if you don’t have a working microphone, you can always use the recorded audio.

2.           Now decimate the audio signal using downsample() and plot the resultant signal in both time and frequency domain. Listen and observe the change in the audio. Kindly note that the decimation in Time domain results in spread of the spectrum in Frequency domain by the same factor.

Keep on doing decimation and plot signals in time domains until aliasing starts to occur and you can feel the change in voice while playing it.

3.           Upsample the audio signal repeatedly by a factor 2 and plot the resultant signal in both time and frequency domain using upsample() function. Note the changes happening to the signal in time domain and corresponding spectrum whenever we add upsample by adding zeros in the time domain. Listen and observe the change in the audio. Do follow the same steps and plot the input signal in time and frequency domain.

4.           Resample the audio signal to a sampling rate of 3/5 by using the functions downsample() and upsample(). Repeat the task for sampling rate of 5/3.

5.           Now find a CD quality background audio from the internet. The sampling rate should be 48 kHz. Now you got to put this background music into recorded audio. Remember you will have to upsample and downsample the two signals to match the two different sampling frequencies.

Analyze the relation between original audio signal and its decimated and interpolated versions based on the magnitude spectrum and by listening the audio after each step. 

Part 1

CODE:

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);           %generate audio data   

Fs =44100;                         %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

subplot(2,1,1)

t = 0:T:(length(data)*T)-T;

plot(t,data)

title('Original Signal in Time Domain')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

FFT= abs(fft(data));

f = (0:length(FFT)-1)*Fs/length(FFT);

plot(f,FFT)

title('Original Signal in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

OUTPUT:

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB


Part 2

CODE:

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);           %generate audio data   

Fs =44100;                         %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

y1 = downsample(data,2);

Fs1=Fs/2;

y2 = downsample(y1,2);

DS_FFT = abs(fft(y2));

T1 = 1/Fs1;

t1 = 0:T1:(length(y1)*T1)-T1;

subplot(2,1,1)

plot(t1,y1)

title('Downsampled Signal by 2 in Time Domain (1)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f1 = (0:length(DS_FFT)-1)*Fs1/length(DS_FFT);

plot(f1,DS_FFT)

title('Downsampled Signal in by 2 Frequency Domain (1)')

xlabel('Frequency')

ylabel('Magnitude') 

figure

Fs2=Fs1/2;

y2 = downsample(y1,2);

DS_FFT2 = abs(fft(y2));

T2 = 1/Fs2;

t2 = 0:T2:(length(y2)*T2)-T2;

subplot(2,1,1)

plot(t2,y2)

title('Again downsampled Signal by 2 in Time Domain (2)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f2 = (0:length(DS_FFT2)-1)*Fs2/length(DS_FFT2);

plot(f2,DS_FFT2)

title('Again downsampled Signal by 2 Frequency Domain (2)')

xlabel('Frequency')

ylabel('Magnitude')

figure

Fs3=Fs2/2;

y3 = downsample(y2,2);

DS_FFT3 = abs(fft(y3));

T3 = 1/Fs3;

t3 = 0:T3:(length(y3)*T3)-T3;

subplot(2,1,1)

plot(t3,y3)

title('Again downsampled Signal by 2 in Time Domain (3)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f3 = (0:length(DS_FFT3)-1)*Fs3/length(DS_FFT3);

plot(f3,DS_FFT3)

title('Again downsampled Signal by 2 Frequency Domain (3)')

xlabel('Frequency')

ylabel('Magnitude')

figure

Fs4=Fs3/2;

y4 = downsample(y3,2);

DS_FFT4 = abs(fft(y4));

T4 = 1/Fs4;

t4 = 0:T4:(length(y4)*T4)-T4;

subplot(2,1,1)

plot(t4,y4)

title('Again downsampled Signal by 2 in Time Domain (4)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f4 = (0:length(DS_FFT4)-1)*Fs4/length(DS_FFT4);

plot(f4,DS_FFT4)

title('Again downsampled Signal by 2 Frequency Domain (4)')

xlabel('Frequency')

ylabel('Magnitude')

sound(y4,Fs4)

figure

Fs5=Fs4/2;

y5 = downsample(y4,2);

DS_FFT5 = abs(fft(y5));

T5 = 1/Fs5;

t5 = 0:T5:(length(y5)*T5)-T5;

subplot(2,1,1)

plot(t5,y5)

title('Again downsampled Signal by 2 in Time Domain (5)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f5 = (0:length(DS_FFT5)-1)*Fs5/length(DS_FFT5);

plot(f5,DS_FFT5)

title('Again downsampled Signal by 2 Frequency Domain (5)')

xlabel('Frequency')

ylabel('Magnitude')

sound(y5,Fs5)

 

OUTPUT:

After 5 times of downsampling by 2, the audio was not recognizable, before that it was atleast understandable. 

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB



 




Part 3

CODE:

 

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);         %generate audio data   

Fs =44100;                       %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

subplot(2,1,1)

t = 0:T:(length(data)*T)-T;

plot(t,data)

title('Original Signal in Time Domain')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

FFT= abs(fft(data));

f = (0:length(FFT)-1)*Fs/length(FFT);

plot(f,FFT)

title('Original Signal in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

 

figure

y1 = upsample(data,2);

Fs1=Fs*2;

y2 = downsample(y1,2);

DS_FFT = abs(fft(y2));

T1 = 1/Fs1;

t1 = 0:T1:(length(y1)*T1)-T1;

subplot(2,1,1)

plot(t1,y1)

title('Upsampled Signal by 2 in Time Domain (1)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f1 = (0:length(DS_FFT)-1)*Fs1/length(DS_FFT);

plot(f1,DS_FFT)

title('Upsampled Signal in by 2 Frequency Domain (1)')

xlabel('Frequency')

ylabel('Magnitude')

 

%

figure

Fs2=Fs1*2;

y2 = upsample(y1,2);

DS_FFT2 = abs(fft(y2));

T2 = 1/Fs2;

t2 = 0:T2:(length(y2)*T2)-T2;

subplot(2,1,1)

plot(t2,y2)

title('Again upsampled Signal by 2 in Time Domain (2)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f2 = (0:length(DS_FFT2)-1)*Fs2/length(DS_FFT2);

plot(f2,DS_FFT2)

title('Again upsampled Signal by 2 Frequency Domain (2)')

xlabel('Frequency')

ylabel('Magnitude')

Fs2

figure

Fs3=Fs2*2;

y3 = upsample(y2,2);

DS_FFT3 = abs(fft(y3));

T3 = 1/Fs3;

t3 = 0:T3:(length(y3)*T3)-T3;

subplot(2,1,1)

plot(t3,y3)

title('Again upsampled Signal by 2 in Time Domain (3)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f3 = (0:length(DS_FFT3)-1)*Fs3/length(DS_FFT3);

plot(f3,DS_FFT3)

title('Again upsampled Signal by 2 Frequency Domain (3)')

xlabel('Frequency')

ylabel('Magnitude')

Fs3

% sound(y3,Fs3)

 

figure

Fs4=Fs3*2;

y4 = upsample(y3,2);

DS_FFT4 = abs(fft(y4));

T4 = 1/Fs4;

t4 = 0:T4:(length(y4)*T4)-T4;

subplot(2,1,1)

plot(t4,y4)

title('Again upsampled Signal by 2 in Time Domain (4)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f4 = (0:length(DS_FFT4)-1)*Fs4/length(DS_FFT4);

plot(f4,DS_FFT4)

title('Again upsampled Signal by 2 Frequency Domain (4)')

xlabel('Frequency')

ylabel('Magnitude')

 

 

figure

Fs5=Fs4*2;

y5 = upsample(y4,2);

DS_FFT5 = abs(fft(y5));

T5 = 1/Fs5;

t5 = 0:T5:(length(y5)*T5)-T5;

subplot(2,1,1)

plot(t5,y5)

title('Again upsampled Signal by 2 in Time Domain (5)')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f5 = (0:length(DS_FFT5)-1)*Fs5/length(DS_FFT5);

plot(f5,DS_FFT5)

title('Again upsampled Signal by 2 Frequency Domain (5)')

xlabel('Frequency')

ylabel('Magnitude')

Fs5

% sound(y5,Fs5)

OUTPUT

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB







Part 4(a)

CODE:

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);         %generate audio data   

Fs =44100;                       %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

subplot(2,1,1)

t = 0:T:(length(data)*T)-T;

plot(t,data)

title('Original Signal in Time Domain')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

FFT= abs(fft(data));

f = (0:length(FFT)-1)*Fs/length(FFT);

plot(f,FFT)

title('Original Signal in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

 

figure

y1 = upsample(data,3);

Fs1=Fs*3;

T1 = 1/Fs1;

t1 = 0:T1:(length(y1)*T1)-T1;

subplot(2,1,1)

plot(t1,y1)

title('Upsampled Signal by 3 in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f1 = (0:length(DS_FFT)-1)*Fs1/length(DS_FFT);

plot(f1,DS_FFT)

title('Upsampled Signal by 3 afterwards in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

 

%

figure

Fs2=Fs1/5;

y2 = downsample(y1,5);

DS_FFT2 = abs(fft(y2));

T2 = 1/Fs2;

t2 = 0:T2:(length(y2)*T2)-T2;

subplot(2,1,1)

plot(t2,y2)

title('Downsampled Signal by 5 in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f2 = (0:length(DS_FFT2)-1)*Fs2/length(DS_FFT2);

plot(f2,DS_FFT2)

title('Downsampled Signal by 5 afterwards in Frequency Domain)')

xlabel('Frequency')

ylabel('Magnitude')

Fs

Fs1

Fs2

OUTPUT:

Sampling Rate of 3/5 is achieved by first upsampling by 3 and then downsampling by 5 afterwards.

Fs is the original sampling frequency which is dropped to 3/5 to 26460.    

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB


 


What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB





 

Part 4(b)

CODE:

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);         %generate audio data   

Fs =44100;                       %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

subplot(2,1,1)

t = 0:T:(length(data)*T)-T;

plot(t,data)

title('Original Signal in Time Domain')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

FFT= abs(fft(data));

f = (0:length(FFT)-1)*Fs/length(FFT);

plot(f,FFT)

title('Original Signal in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

 

figure

y1 = upsample(data,5);

Fs1=Fs*5;

T1 = 1/Fs1;

t1 = 0:T1:(length(y1)*T1)-T1;

subplot(2,1,1)

plot(t1,y1)

title('Upsampled Signal by 5 in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f1 = (0:length(DS_FFT)-1)*Fs1/length(DS_FFT);

plot(f1,DS_FFT)

title('Upsampled Signal by 5 in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

 

%

figure

Fs2=Fs1/3;

y2 = downsample(y1,3);

DS_FFT2 = abs(fft(y2));

T2 = 1/Fs2;

t2 = 0:T2:(length(y2)*T2)-T2;

subplot(2,1,1)

plot(t2,y2)

title('Downsampled Signal by 3 afterwards in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f2 = (0:length(DS_FFT2)-1)*Fs2/length(DS_FFT2);

plot(f2,DS_FFT2)

title('Downsampled Signal by 3 afterwards in Frequency Domain)')

xlabel('Frequency')

ylabel('Magnitude')

Fs

Fs1

Fs2

OUTPUT:

Sampling Rate of 5/3 is achieved by first upsampling by 5 and then downsampling by 3 afterwards.

Fs is the original sampling frequency which is dropped to 5/3 to 73500.    

 

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB
What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB




 




Part 5

CODE:

rec = audiorecorder(44100,24,1); %create an object for recording

record(rec,3);                   %record for 3 seconds

data =getaudiodata(rec);         %generate audio data   

Fs =44100;                       %sampling frequency

T = 1/Fs;

% sound(data,Fs)

figure

subplot(2,1,1)

t = 0:T:(length(data)*T)-T;

plot(t,data)

title('Original Recorded Signal in Time Domain')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

FFT= abs(fft(data));

f = (0:length(FFT)-1)*Fs/length(FFT);

plot(f,FFT)

title('Original Recorded Signal in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

Fs

figure

 

y1 = upsample(data,160);

Fs1=Fs*160;

T1 = 1/Fs1;

t1 = 0:T1:(length(y1)*T1)-T1;

subplot(2,1,1)

plot(t1,y1)

title('Upsampled Recorded Signal by 160 in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f1 = (0:length(DS_FFT)-1)*Fs1/length(DS_FFT);

plot(f1,DS_FFT)

title('Upsampled Recorded Signal by 160 in Frequency Domain')

xlabel('Frequency')

ylabel('Magnitude')

Fs1

%

figure

Fs2=Fs1/147;

y2 = downsample(y1,147);

DS_FFT2 = abs(fft(y2));

T2 = 1/Fs2;

t2 = 0:T2:(length(y2)*T2)-T2;

subplot(2,1,1)

plot(t2,y2)

title('Downsampled Recorded Signal by 147 afterwards in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f2 = (0:length(DS_FFT2)-1)*Fs2/length(DS_FFT2);

plot(f2,DS_FFT2)

title('Downsampled Recorded by 147 afterwards in Frequency Domain)')

xlabel('Frequency')

ylabel('Magnitude')

 

Fs2

% % sound(y2,Fs2)

 

[z,Fz] = audioread('FreestudyStrom.wav');

samples = [1,3*Fz];                                   %extract 3 seconds

clear y Fs

[z,Fz] 

audioread('FreestudyStrom.wav',samples);            %updated Sampling

z1 =z(:,1);

DS_FFT3 = abs(fft(z1));

Fz

I = audioinfo('FreestudyStrom.wav')

 

figure

T3 = 1/Fz;

t3 = 0:T3:(length(z)*T3)-T3;

subplot(2,1,1)

plot(t3,z1)

title('CD Quality Signalin Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f3 = (0:length(DS_FFT3)-1)*Fz/length(DS_FFT3);

plot(f3,DS_FFT3)

title('CD Quality Signal Frequency Domain ')

xlabel('Frequency')

ylabel('Magnitude')

Fs3

Mixed = (z1*0.1)+y2;

sound(Mixed,Fz)

figure

Fmixed=Fz;

DS_FFTm = abs(fft(Mixed));

T4 = 1/Fmixed;

t4 = 0:T4:(length(Mixed)*T4)-T4;

subplot(2,1,1)

plot(t4,Mixed)

title('Mixed Signal in Time Domain ')

xlabel('Time')

ylabel('Amplitude')

subplot(2,1,2)

f4 = (0:length(DS_FFTm)-1)*Fmixed/length(DS_FFTm);

plot(f4,DS_FFTm)

title('Mixed Signal Frequency Domain ')

xlabel('Frequency')

ylabel('Magnitude')

 

OUTPUT:

For two audios to be mixed, first we have to make their sampling rates equal. For that we’ll make the sampling frequency of our recorded audio to 48kHz i.e the sampling frequency of CD Quality audio. Therefore, the recorded audio is first upsampled by 160 and then downsampled by 147 afterwards to get a sampling frequency of 48k Hz. Now, both audios are at same sampling frequency and therefore can be added together. The amplitude of CD Quality audio is dropped to let it go to the background and our audio to be on the foreground. 

 

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB






02:Polyphase:

1.      You have to take the audio signal recorded earlier at 44.1 kHz and downsample it by a factor of 4. Before you drop the samples, you have to pass it through the anti-aliasing filter of a specific cutoff. You can design the filter by using the following command:

b = fir1(n,Wn)

where n is the order of the lowpass filter with cutoff Wn. In this case use n=39.

1.      Now you have apply the concepts of polyphase filtering to downsample the audio signal by a factor of 4 using the coefficients of the filter generated earlier. Use figures to show that the outputs of antialiasing filtering and downsampling and downsampling using polyphase filtering is same.

2.      Now upsample this signal by a factor of 4 by adding zeros in the signal and pass it through the reconstruction filter.

3.      Upsample the signal by a factor of 4 by using polyphase filtering. Use figures to show that the outputs of upsampling and reconstruction filter and polyphase filtered signal is same.

Part 1 : Decimation

CODE:

[x,Fs] = audioread('FreestudyStrom.wav');

x = x(:,1);

% Usual Decimation

D = 4;                      %downsampling factor

len  = 40;                  %filter length

wc = 1/D;                   %cut-off frequency is pi/4.

filter = fir1(len-1, wc);   %decimation filter

y = conv(filter, x);        %convolve input and the filter

len_y = length(y);

yd = downsample(y,4);       %downsample the filtered output by 4

%yd is the output (1)

% Decimation using polyphase decomposition

%slpit the filter h into D subfilters 

f0 = filter(1:4:len);

f1 = filter(2:4:len);

f2 = filter(3:4:len);

f3 = filter(4:4:len);

%slpit the input x into D subinputs

len_x = length(x);

x0 = x(1:4:len_x);

x1 = x(2:4:len_x);

x2 = x(3:4:len_x);

x3 = x(4:4:len_x);

%convolving each subinput to corresponding subfilter

y0 = conv(x0, f0);

y1 = conv(x1, f1);

y2 = conv(x2, f2);

y3 = conv(x3, f3);

%finding lengths of the outputs

N0 = length(y0);

N1 = length(y1);

N2 = length(y2);

N3 = length(y3);

N = [N0,N1, N2,N3];                     

n = min(N);                                 %find minimum length output

yp = y0(1:n) + y1(1:n) +y2(1:n) +y3(1:n);   %add all suboutputs to get final result(2)

figure(1)

subplot(511)

stem(filter)

ylabel('h');

title('Original filter and Sub-filters')

subplot(512)

stem(f0)

ylabel('f0');

subplot(513)

stem(f1)

ylabel('f1');

subplot(514)

stem(f2)

ylabel('f2');

subplot(515)

stem(f3)

ylabel('f3');

figure(2);

[X, wx] = freqz(x, 1, len_y, 'whole');

[Y, wy] = freqz(y, 1, len_y, 'whole');

[H, wh] = freqz(filter, 1, 1024, 'whole');

subplot(311)

plot(wx/(2*pi), abs(X));

title('Original Sequence');

subplot(312)

plot (wh/(2*pi), abs(H));

title('Anti aliasing Filter')

subplot(313)

plot(wy/(2*pi), abs(Y), 'r');

title('Sequence after filtering');

figure(3);

subplot(511), plot(x), ylabel('x')

title('Original Input and Sub-Inputs')

subplot(512), plot(x0), ylabel('x0')

subplot(513), plot(x1), ylabel('x1')

subplot(514), plot(x2), ylabel('x2')

subplot(515), plot(x3), ylabel('x3')

figure (4);

subplot(211)

plot(yd)

title('Output by Antialiasing Filter and Decimator');

subplot(212)

plot(yp)

title('Output by Polyphase Decomposition');

figure(5);

Yd=abs(fft(yd));

Yp=abs(fft(yp));

subplot(211)

plot(Yd)

title('Output by Antialiasing Filter and Decimator');

subplot(212)

plot(Yp, 'r')

title('Output by Polyphase Decomposition');

 

OUTPUT:

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB


What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB
What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB





 




Part 2 : Interpolation

CODE:

[x,Fs] = audioread('C:\FreestudyStrom.wav');

x = x(:,1);

len_x = length(x);

% Usual Interpolation

L = 4;                      %upsampling factor

len  = 40;                  %filter length

wc = 1/L;                   %cut-off frequency is pi/4.

filter = fir1(len-1, wc);   %interpolating filter

yu = upsample(x,4);         %upsample the input signal by 4

y = conv(filter, yu);       %convolve input and the filter

len_y = length(y);

%y is the output (1)

%Interpolation using polyphase Interpolation

%slpit the filter h into L subfilters 

f0 = filter(1:4:len);

f1 = filter(2:4:len);

f2 = filter(3:4:len);

f3 = filter(4:4:len);

%convolving input to each subfilter

y0 = conv(x, f0);

y1 = conv(x, f1);

y2 = conv(x, f2);

y3 = conv(x, f3);

y0=upsample(y0,4);

y1=upsample(y1,4);

y2=upsample(y2,4);

y3=upsample(y3,4);

%add all suboutputs to get final result(2)

yp = y0 + delayseq(y1,1)+delayseq(y2,2)+delayseq(y3,3);

figure(1)

subplot(511)

stem(filter)

ylabel('h');

title('Original filter and Sub-filters')

subplot(512)

stem(f0)

ylabel('f0');

subplot(513)

stem(f1)

ylabel('f1');

subplot(514)

stem(f2)

ylabel('f2');

subplot(515)

stem(f3)

ylabel('f3');

figure(2);

[X, wx] = freqz(x, 1, len_y, 'whole');

[Y, wy] = freqz(yu, 1, len_y, 'whole');

[H, wh] = freqz(filter, 1, 1024, 'whole');

subplot(311)

plot(wx/(2*pi), abs(X));

title('Original Sequence');

subplot(312)

plot (wh/(2*pi), abs(H));

title('Interpolating Filter')

subplot(313)

plot(wy/(2*pi), abs(Y), 'r');

title('Sequence after Filtering');

figure (3);

subplot(211)

plot(yu)

title('Output by Interpolation Filter and Interpolation');

subplot(212)

plot(yp)

title('Output by Polyphase Interpolation');

figure(4);

Yu=abs(fft(y));

Yp=abs(fft(yp));

subplot(211)

plot(Yu)

title('Output by Interpolation Filter and Interpolator');

subplot(212)

plot(Yp, 'r')

title('Output by Polyphase Interpolation');

 

OUTPUT:

What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB


What Is Interpolation and Decimation ? How to do Interpolation and Decimation in MATLAB






No comments:

Post a Comment

Pages