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=2pi, ws=2pi*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

Mid-rise type quantizer