还是一样,先贴程序
%sampling and quantisation
clc;
clear;
%sampling
A = 1; % sinusoid's amplitude
f = 2;% frequency Hz
phase = 0*pi;% phase Rad
f_sample = 1000;%sample frequecy
t_start = 0;
t_stop = 1;
t = t_start:1/f_sample:t_stop;
y = A*sin(2*pi*f*t+phase); % y includes all sampled value
figure(1);
plot(t,y);
xlabel('t');
grid on;
%quantisation
n = 2; % n bits represent the number of 2^n combinations
M = 2^n;% quantization level
delta_v = 2*A/M; % quantization interval
m_i = zeros(1,M);% mi = a + i*delta_v, quantization_endpoint_value
for i = 1:M
m_i(i) = -A + i*delta_v;
end
m_i_ahead = [-A m_i(1:end-1)]; % mi-1
% quantization_value qi = (mi+mi-1)/2
q_i = (m_i + m_i_ahead)/2;
% find the sampled valued which is the nearst to quantization value
y_hat = repmat(y,M,1);
q_i_hat = repmat(q_i.',1,size(y_hat,2));
nearst_value = abs(y_hat - q_i_hat);
[min_value min_location] = min(nearst_value,[],1);
min_location_hat = min_location-1;%because the value of min_location is from 1 to 8, change
% it from 0 to 7, in order to turn it to 000,001,010...
y_digital = dec2bin(min_location_hat,n);
% plot quantization_value
hold on;
plot(t,q_i_hat(min_location),'r');
legend('sampled value','quantized value')
程序的第一部分我在之前发的一个帖子里说明过,就是一个时间上离散但是数值上是连续的信号(抽样过的信号),那么接下来我们要做的就是给信号量化成数字信号。具体的理论书上都有,不在这细谈,可以书上查找查找,我这里仿真的是最简单的均与量化,至于非均匀量化,看懂原理,花费些时间,都可以编出来。
量化电平数 M = 2^n;
均匀量化间隔 delta_v = (b-a)/M;
量化区间终点 mi = a+i*delta_v ;
量化区间中点 qi = (mi+mi_1)/2, i = 1,2,3...M,