Experiment 1
Experiment 1
Kanishk Devatwal
February 2025
1 Experiment-1
Question 1
To Generate and plot samples of x(t) = 2sin(2πf t)
f = 1kHz
Fs = 48kHz
Duration = 1Sec
t = 0 : 1 / Fs : d u r a t i o n ;
x t = 2 ∗ s i n (2 ∗ pi ∗ f ∗ t ) ;
figure ;
plot ( t plot , x plot , ’b ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
t i t l e ( ’ Generated S i g n a l x ( t ) f o r 5 Cycles ’ ) ;
g r i d on ;
legend ( ’ x ( t ) = 2 s i n (2\ pi f t ) ’ ) ;
1
Figure 1: Sine wave samples for 1 Second Duration
Question 2
Converting the samples to fixed point formats of Q(2, 14), Q(4, 12), Q(8, 4)
MALAB Code:
x t = x t ( 1 : num samples ) ;
Q2 14 = round ( x t ∗ 2 ˆ 1 4 ) ;
Q4 12 = round ( x t ∗ 2 ˆ 1 2 ) ;
Q8 4 = round ( x t ∗ 2 ˆ 4 ) ;
x Q2 14 = Q2 14 / 2 ˆ 1 4 ;
x Q4 12 = Q4 12 / 2 ˆ 1 2 ;
x Q8 4 = Q8 4 / 2 ˆ 4 ;
figure ;
subplot (3 ,1 ,1);
p l o t ( t ( 1 : num samples ) , x Q2 14 , ’ r ’ ) ;
t i t l e ( ’ Fixed−P oi nt R e p r e s e n t a t i o n Q( 2 , 1 4 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
g r i d on ;
subplot (3 ,1 ,2);
2
p l o t ( t ( 1 : num samples ) , x Q4 12 , ’ g ’ ) ;
t i t l e ( ’ Fixed−P oi nt R e p r e s e n t a t i o n Q( 4 , 1 2 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
g r i d on ;
subplot (3 ,1 ,3);
p l o t ( t ( 1 : num samples ) , x Q8 4 , ’ b ’ ) ;
t i t l e ( ’ Fixed−P oi nt R e p r e s e n t a t i o n Q( 8 , 4 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
g r i d on ;
Question 3
Plot the Quantized signal vs Original signal:
Matlab Code:
Q2 14 = round ( x t ∗ 2 ˆ 1 4 ) / 2 ˆ 1 4 ;
Q4 12 = round ( x t ∗ 2 ˆ 1 2 ) / 2 ˆ 1 2 ;
Q8 4 = round ( x t ∗ 2 ˆ 4 ) / 2 ˆ 4 ;
figure ;
subplot (3 ,1 ,1);
3
p l o t ( t p l o t , x t , ’ k ’ , ’ LineWidth ’ , 1 . 2 ) ; h o l d on ;
p l o t ( t p l o t , Q2 14 , ’ r − − ’);
t i t l e ( ’Q( 2 , 1 4 ) vs O r i g i n a l ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
l e g e n d ( ’ O r i g i n a l ’ , ’Q( 2 , 1 4 ) ’ ) ;
g r i d on ;
subplot (3 ,1 ,2);
p l o t ( t p l o t , x t , ’ k ’ , ’ LineWidth ’ , 1 . 2 ) ; h o l d on ;
p l o t ( t p l o t , Q4 12 , ’ g − − ’);
t i t l e ( ’Q( 4 , 1 2 ) vs O r i g i n a l ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
l e g e n d ( ’ O r i g i n a l ’ , ’Q( 4 , 1 2 ) ’ ) ;
g r i d on ;
subplot (3 ,1 ,3);
p l o t ( t p l o t , x t , ’ k ’ , ’ LineWidth ’ , 1 . 2 ) ; h o l d on ;
p l o t ( t p l o t , Q8 4 , ’ b− − ’);
t i t l e ( ’Q( 8 , 4 ) vs O r i g i n a l ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Amplitude ’ ) ;
l e g e n d ( ’ O r i g i n a l ’ , ’Q( 8 , 4 ) ’ ) ;
g r i d on ;
4
Figure 3: Quantized Signal vs Original signal
Question 4
Plot errors in each case:
Matlab Code:
Q2 14 = round ( x t ∗ 2 ˆ 1 4 ) / 2 ˆ 1 4 ;
Q4 12 = round ( x t ∗ 2 ˆ 1 2 ) / 2 ˆ 1 2 ;
Q8 4 = round ( x t ∗ 2 ˆ 4 ) / 2 ˆ 4 ;
e r r o r Q 2 1 4 = x t − Q2 14 ;
e r r o r Q 4 1 2 = x t − Q4 12 ;
e r r o r Q 8 4 = x t − Q8 4 ;
figure ;
subplot (3 ,1 ,1);
plot ( t plot , error Q2 14 , ’ r ’ ) ;
t i t l e ( ’ Q u a n t i z a t i o n E r r o r f o r Q( 2 , 1 4 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Error ’ ) ;
g r i d on ;
subplot (3 ,1 ,2);
plot ( t plot , error Q4 12 , ’g ’ ) ;
5
t i t l e ( ’ Q u a n t i z a t i o n E r r o r f o r Q( 4 , 1 2 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Error ’ ) ;
g r i d on ;
subplot (3 ,1 ,3);
plot ( t plot , error Q8 4 , ’b ’ ) ;
t i t l e ( ’ Q u a n t i z a t i o n E r r o r f o r Q( 8 , 4 ) ’ ) ;
x l a b e l ( ’ Time ( s ) ’ ) ;
y l a b e l ( ’ Error ’ ) ;
g r i d on ;
6
Question 5
mean(x[n]2 )
SQN R =
mean(e[n]2 )
Matlab Code:
signal power = mean ( x t . ˆ 2 ) ;
error power Q2 1 4 = mean ( e r r o r Q 2 1 4 . ˆ 2 ) ;
error power Q4 1 2 = mean ( e r r o r Q 4 1 2 . ˆ 2 ) ;
error power Q8 4 = mean ( e r r o r Q 8 4 . ˆ 2 ) ;
SQNR Q2 14 = 10 ∗ l o g 1 0 ( s i g n a l p o w e r / e r r o r p o w e r Q 2 1 4 ) ;
SQNR Q4 12 = 10 ∗ l o g 1 0 ( s i g n a l p o w e r / e r r o r p o w e r Q 4 1 2 ) ;
SQNR Q8 4 = 10 ∗ l o g 1 0 ( s i g n a l p o w e r / e r r o r p o w e r Q 8 4 ) ;