# Confusion implementing Quantization in MATLAB?

I am quantizing a continuous time signal in three different cases(using different number of bits)

But Why i am getting same quantized signal in xq2 and xq3,despite using different values of D(Quantization step), my code is below

clc;clear all;close all;
n=0:19; %defining range for discrete time
fs=100;%defining sampling frequency
Ts=1/fs;%calculating sampling interval
t=0:Ts:2; %defining continous time range
f=10;%defining signal frequency
m1=4%defining number of bits for case 1
m2=5%defining number of bits for case 2
m3=6%defining number of bits for case 3
L1=2^m1 %calculating number of quantization levels for case 1
L2=2^m2%calculating number of quantization levels for case 2
L3=2^m3%calculating number of quantization levels for case 3
x=cos(2*pi*f*t)%defining continous time (analog) signal
x_s=cos(2*pi*f*(n*Ts))%defining discrete time signal
D1=[(max(x)-min(x))]/L1%calculating step stize for case 1
xq1=quant(x,D1)%determing quantized signal for case
D2=[(max(x)-min(x))]/L2%calculating step stize for case 2
xq2=quant(x,D2)%determing quantized signal for case 2
D3=[(max(x)-min(x))]/L3%calculating step stize for case 2
xq3=quant(x,D3)%determing quantized signal for case 2

• have you plotted your signals pre-quantization? You'll notice a pattern! Mar 7, 2022 at 11:37

Why i am getting same quantized signal in xq2 and xq3,

Very poor choice of test signal.

A 10 Hz cosine wave sampled at 100 Hz has only 3 different values that keep repeating (in flipped in sign and order). You are not quantizing a signal, you are quantizing $$1$$, $$\cos(\pi/5)$$ and $$\cos(2\pi/5)$$ over and over again. Given your method, all levels will quantize $$1$$ correctly, so you are really testing only TWO different numbers.

For levels 2 and 3 these just happen to quantize the same.

Better test signals would be a ramp, noise, or a sine with a frequency that's not an integer divider of the sample rate.