PRE LAB #2
% This script creates a signal, and then quantizes it to a specified number
% of bits. It then calculates the quantization error. clc clear all close all
Change the #of bits means change in resolution
Change the #of bits means change in resolution fprintf(' Sampling and Quantization\n'); b=5; % Number of bits.
N=100; % Number of samples in final signal. n=0:(N-1); %Index
Change the value of N means change in sampling rate of sig
Change the value of N means change in sampling rate of sig % Choose the input type. choice = questdlg('Choose input','Input',... 'Sine','Sawtooth','Random','Random'); fprintf('Bits = %g, levels = %g, signal = %s.\n', b, 2^b, choice); % Create the input data sequence. switch choice case 'Sine' x=sin(2*pi*n/N); case 'Sawtooth' x=sawtooth(2*pi*n/N); case 'Random' x=randn(1,N); % Random data x=x/max(abs(x)); % Scale to +/- 1 end % Signal is restricted to between -1 and +1. x(x>=1)=(1-eps); % Make signal from -1 to just less than 1. x(x<-1)=-1; % Quantize a signal to "b" bits. xq=floor((x+1)*2^(b-1)); % Signal is one of 2^n int values (0 to 2^n-1) xq=xq/(2^(b-1)); % Signal is from 0 to 2 (quantized) xq=xq-(2^(b)-1)/2^(b); % Shift signal down (rounding) xe=x-xq; % Quantization error stem(x,'b'); hold on; stem(xq,'r'); hold on; stem(xe,'g'); legend('exact','quantized','error','Location','Southeast') title(sprintf('Signal, Quantized signal and Error for %g bits, %g quantization levels',b,2^b)); hold off
if we increase the the # of bits or resolution then error between quantized and sampled signal decrease and vice versa.
For # of bits=5 for # of bits=3
If we further increase the # of bits error signal approaches to zero.
For # of bits =8
Question # 2
We change the sampling rate of signal in above given code. If we change the value of N, then we can observe under sampled or over sampled signal. For N =10 (under sampled signal) for N= 150 (over sampled)
As we over sampled the signal error signal increases.