Assignment 1
Quantization Noise
Jay Soni
12040700
January 16, 2024
1 Generate and plot samples of x(t)=2sin(2*pi*f*t),
f=1kHz, Fs=48kHz for 1 sec duration. (Plot
can be done only for 5 cycles)
Figure 1: Five Cycles of Original Signal
1
MATLAB Code
1 % Given Parameters
2 f = 1000; % Frequency
3 Fs = 48000; % Sampling Frequency
4 duration = 1; % Duration
5 t = 0:1/ Fs : duration ; % Time Vector
6 x = 2* sin (2* pi * f * t ) ; % Given Signal
7
8 % Plot only for 5 cycles ( adjust the range
accordingly )
9 cycles_to_plot = 5;
10 samples_to_plot = cycles_to_plot * Fs / f ;
11
12 % Plot the signal for the specified duration
13 plot ( t (1: samples_to_plot ) , x (1: samples_to_plot ) ) ;
14
15 % Add labels and title
16 xlabel ( ’ Time ( seconds ) ’) ;
17 ylabel ( ’ Amplitude ’) ;
18 title ( ’ Sine Wave Signal ’) ;
19
20 % Display the grid
21 grid on ;
22
23 % Show the plot
24 legend ( ’ Signal ’) ;
Code To Plot The Signal
2 Convert the samples to fixed point formats of
Q(2,14), Q(4,12), Q(8,4)
MATLAB Code
1 % Q (2 ,14)
2 q2_14 = fi (x ,1 ,16 ,12) ;
3
4 % Q (4 ,12)
5 q4_12 = fi (x ,1 ,16 ,12) ;
6
7 % Q (8 ,4)
8 q8_4 = fi (x ,1 ,12 ,4) ;
2
Code To Convert To Fixed Point Format
3 Plot the quantized signals vs the original sig-
nal
Figure 2: Original v/s Quantized Signal for Q(2,14)
Figure 3: Original v/s Quantized Signal for Q(4,12)
Figure 4: Original v/s Quantized Signal for Q(8,4)
MATLAB Code
1 figure ;
2
3 subplot (3 ,1 ,1) ;
3
4 plot ( t (1: samples_to_plot ) , x (1: samples_to_plot ) , ’
b - ’ , t (1: samples_to_plot ) , q2_14 (1:
samples_to_plot ) , ’r : ’) ;
5 xlabel ( ’ Time ( seconds ) ’) ;
6 ylabel ( ’ Amplitude ’) ;
7 title ( ’ Original vs Q (2 ,14) Quantized Signal ’) ;
8 legend ( ’ Original ’ , ’ Quantized ’) ;
9 grid on ;
10
11 subplot (3 ,1 ,2) ;
12 plot ( t (1: samples_to_plot ) , x (1: samples_to_plot ) , ’
b - ’ , t (1: samples_to_plot ) , q4_12 (1:
samples_to_plot ) , ’g : ’) ;
13 xlabel ( ’ Time ( seconds ) ’) ;
14 ylabel ( ’ Amplitude ’) ;
15 title ( ’ Original vs Q (4 ,12) Quantized Signal ’) ;
16 legend ( ’ Original ’ , ’ Quantized ’) ;
17 grid on ;
18
19 subplot (3 ,1 ,3) ;
20 plot ( t (1: samples_to_plot ) , x (1: samples_to_plot ) , ’
b - ’ , t (1: samples_to_plot ) , q8_4 (1:
samples_to_plot ) , ’y : ’) ;
21 xlabel ( ’ Time ( seconds ) ’) ;
22 ylabel ( ’ Amplitude ’) ;
23 title ( ’ Original vs Q (8 ,4) Quantized Signal ’) ;
24 legend ( ’ Original ’ , ’ Quantized ’) ;
25 grid on ;
Code To Plot Original v/s Quantized Signals
4 Plot the errors in each case
Figure 5: Original v/s Error for Q(2,14)
4
Figure 6: Error for Q(4,12)
Figure 7: Error for Q(8,4)
MATLAB Code
1 error_q2_14 = x - double ( q2_14 ) ;
2 error_q4_12 = x - double ( q4_12 ) ;
3 error_q8_4 = x - double ( q8_4 ) ;
4
5 figure ;
6
7 subplot (3 ,1 ,1) ;
8 plot ( t (1: samples_to_plot ) , error_q2_14 (1:
samples_to_plot ) , ’r ’) ;
9 xlabel ( ’ Time ( seconds ) ’) ;
10 ylabel ( ’ Error ’) ;
11 title ( ’ Error for Q (2 ,14) ’) ;
12 grid on ;
13
14 subplot (3 ,1 ,2) ;
15 plot ( t (1: samples_to_plot ) , error_q4_12 (1:
samples_to_plot ) , ’g ’) ;
16 xlabel ( ’ Time ( seconds ) ’) ;
17 ylabel ( ’ Error ’) ;
18 title ( ’ Error for Q (4 ,12) ’) ;
19 grid on ;
20
5
21 subplot (3 ,1 ,3) ;
22 plot ( t (1: samples_to_plot ) , error_q8_4 (1:
samples_to_plot ) , ’y ’) ;
23 xlabel ( ’ Time ( seconds ) ’) ;
24 ylabel ( ’ Error ’) ;
25 title ( ’ Error for Q (8 ,4) ’) ;
26 grid on ;
Code To Plot Errors
5 Find the SQNR for each case
Figure 8: MATLAB Output
MATLAB Code
6
1 SQNR_q2_14 = mean ( abs ( x ) .^2) / mean ( abs (
error_q2_14 ) .^2) ;
2 SQNR_q4_12 = mean ( abs ( x ) .^2) / mean ( abs (
error_q4_12 ) .^2) ;
3 SQNR_q8_4 = mean ( abs ( x ) .^2) / mean ( abs ( error_q8_4 )
.^2) ;
4
5 disp ( ’ SQNR for Q (2 ,14) : ’) ;
6 disp ( SQNR_q2_14 ) ;
7
8 disp ( ’ SQNR for Q (4 ,12) : ’) ;
9 disp ( SQNR_q4_12 ) ;
10
11 disp ( ’ SQNR for Q (8 ,4) : ’) ;
12 disp ( SQNR_q8_4 ) ;
Code To Finf SQNR