Mid-rise type quantizer

A mid-rise type quantizer for my DSP lab course. The question is as below.
Generate a discrete-time sinusoidal signal x[n] with the SinSamples() function implemented in preliminary work of experiment 1, with the parameters: A=3, w=2*pi, ws=2*pi*50, .θ=0, d=2sec. Implement 3-bit midrise type quantizer. Make the reconstruction levels be spaced so as to span the entire amplitude range of the signal. You may use the maximum amplitude of the signal in designing reconstruction levels. Plot original signal, quantized version, and quantization error. Calculate output signal to noise ratio in dB (all signal to noise ratios must be calculated in dB).

Generate a discrete-time sinusoidal signal x[n] with the SinSamples() function implemented in preliminary work of experiment 1, with the parameters: A=3, w=2*pi, ws=2*pi*50, .θ=0, d=2seca) Implement 3-bit midrise type quantizer. Make the reconstruction levels be spaced so as to span the entire amplitude range of the signal. You may use the maximum amplitude of the signal in designing reconstruction levels. Plot original signal, quantized version, and quantization error. Calculate output signal to noise ratio in dB (all signal to noise ratios must be calculated in dB).

A=3;
w=2*pi
w_s=2*pi*50;
d=2;
teta=0;
 
f = w/(2*pi);
T = 1/f;
tmin = 0;
dt = T/100;
dt1 = 1/(w_s/(2*pi));
t = tmin:dt:d;
t1 = tmin:dt1:d;
x = A*sin(w*t+teta);
x1 = A*sin(w*t1+teta);
subplot(3,1,1);
plot(t,x,'r');
hold on
stem(t1,x1);
title('The sampled and original signal');
xlabel(''), ylabel('amplitude');
grid on;
hold on
 
% Quatization part (midrise)
bit=3; %number of bits that will be used
signal=x1; %get the signal
m_max=max(abs(signal)); %find the highest magnitude used
delta=(2*m_max)/(2^bit) ;  %our step size
k_max=(2^bit)/2; %how many levels we have in one side of the quatization graph
for i=1:length(signal)
    for k=0:1:(k_max-1)
        if (((k*delta)<=abs(signal(i)))&&(abs(signal(i))<=((k+1)*delta)))
            if(signal(i)>0)
                new_signal(i)=(0.5+k)*delta;
            elseif(signal(i)<0)
                new_signal(i)=(-0.5-k)*delta;
            elseif(signal(i)==0)    
                new_signal(i)=0;
            end 
        end
 
    end
end
subplot(3,1,2);
stem(t1,new_signal);
xlabel(''), ylabel('amplitude');
grid on;
hold on
 
error=signal-new_signal;
subplot(3,1,3);
stem(t1,error);
xlabel(''), ylabel('amplitude');
grid on;
hold on

And the output is as below;

Rebuilding

As you may have noticed. I have renewed the site to a more up-lifting theme. I hope you like it. I certainly do. Reading is a little easier in this theme.

I am also trying out Windows Live Writer. I did try other offline writers however they weren’t really that good. Thinking smile <-Trying out a emoticon.

I haven’t been able to post anything lately. I was busy studying. Oh yeah speaking of studying. My paper has been accepted by IEEE Transactions Consumer Electronics. It will be published in the November 2010 issue. I have been informed I will also receive a free copy. Winking smile

So, updated my CV and updated my site. Continue to follow my site and you may receive a surprise.