# Sampling Rate Conversion Using MATLAB

The Processing of discrete time signal at different sampling rates in different parts of a system is called as multirate Digital Signal Processing (DSP). The process of converting a signal from one sampling rate to another sampling rate is called as sampling rate conversion.
In digital domain sampling rate conversion two methods are used:
• Decimation or Down Sampling.
• Interpolation or Up Sampling.

Decimation or Down Sampling is a process of reducing the sampling rate by an integer factor D. Interpolation or Up Sampling is the process of increasing the sampling rate by an integer factor I.

Advantages of Signal Rate Conversion techniques:-
• Reduction in number of computations.
• Reduction in memory requirements (or Storage) for filter coefficiemts and intermediate results.
• Reduction in the order of the system.
• The finite word length effects are reduced.

## Down Sampling (or Decimation):

Let, x(n)= Discrete time signal
D= Sampling rate reduction factor(and Dis an integer)
Now, x(Dn)= Down sampled version of x(n)
The device which performs the process of down sampling is called as downsampler (or decimator).
Symbollically, the downsampler can be represented as shown in figure below

### EXAMPLE:

Consider the discrete time signal shown in figure. D=2

Solution:-

Given from signal:
x(n)=[1,-1,1,-1,2,-2,2,-2,3,-3,3,-3]

when n=0, x(n)= x(0)= 1
When n=1, x(n)= x(1)= -1
When n=2, x(n)= x(2)= 1
When n=3, x(n)= x(3)= -1
When n=4, x(n)= x(4)= 2
When n=5, x(n)= x(5)= -2
When n=6, x(n)= x(6)= 2
When n=7, x(n)= x(7)= -2
When n=8, x(n)= x(8)= 3
When n=9, x(n)= x(9)= -3
When n=10, x(n)= x(10)= 3
When n=11, x(n)= x(11)= -3

Sampling rate reduction factor, D= 2.
Let xD2(n)=x(Dn)=x(2n)
When n=0, xD2(n)= xD2(0)= x(0)= 1
When n=1, xD2(n)= xD2(1)= x(2)= 1
When n=2, xD2(n)= xD2(2)= x(4)= 2
When n=3, xD2(n)= xD2(3)= x(6)= 2
When n=4, xD2(n)= xD2(4)= x(8)= 3
When n=5, xD2(n)= xD2(5)= x(10)= 3

t x(2n)=[1,1,2,2,3,3]
Using this equation the decimated signal of x(n) by reduction factor 2, is drawn below

### MATLAB CODE (to get the above output):

```xn=[1,-1,1,-1,2,-2,2,-2,3,-3,3,-3];
N=length(xn);
n=0:1:N-1;
stem(n,xn);
D=2;
xDn=xn(1:D:N);
n1=1:1:N/D;
figure;
stem(n1-1,xDn)
```

### OUTPUT:

xn =
1 -1 1 -1 2 -2 2 -2 3 -3 3 -3
N =
12
xDn =
1 1 2 2 3 3

## Up Sampling (or Interpolation)

Let, x(n)= Discrete time signal
I= Interpolation factor
Now, x(n/I)= Upsampled version on x(n).

### EXAMPLE:

Consider the discrete time signal,
x(n)=[1,-1,2,-2]
Determine the upsampled version of the signal with I=2
Solution:
Given that x(n)=[ 1,-1,2,-2]

When n=0, x(n)= x(0)= 1
When n=1, x(n)= x(1)= -1
When n=2, x(n)= x(2)= 2
When n=3, x(n)= x(3)= -2
Sampling rate multiplication factor, I= 2
On solving similar to decimation we get,
x(n/2)=[1,0,-1,0,2,0,-2,0]

### MATLAB CODE (to get the above output):

```xn=[1 -1 2 -2]
N=length(xn)
n=0:1:N-1
stem(n,xn);
I=2
xIn=[zeros(1,I*N)]
n1=1:1:N*I
j=1:I:I*N
xIn(j)=xn
figure;
stem(n1-1,xIn)
```

### OUTPUT:

xn =
1 -1 2 -2
N =
4
n =
0 1 2 3
I =
2
xIn =
0 0 0 0 0 0 0 0
n1 =
1 2 3 4 5 6 7 8
j =
1 3 5 7
xIn =
1 0 -1 0 2 0 -2 0