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;

Fork me on GitHub

Plotting and finding the magnitude of a wav file in MATLAB[edited]

Edited: Changed code to a more correct version!
The question was;

Load the file sound1.wav(Download from here => SoundWav (125) ) (You would use MATLAB command ‘wavread’ to loadthis file. Use MATLAB help to learn the usage of ‘wavread’). This file contains a portion of speech waveform. Take the first 512 point the signal, plot the waveformand its magnitude spectrum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
%call it like question_8('sound1.wav')
function question_8(url)
[x,fs]=wavread(url);
new_x=x(1:512);
subplot(2,1,1);
stem(new_x);
title('The sampled sound signal');
xlabel('time'), ylabel('amplitude');
grid on;
hold on
 
 
subplot(2,1,2)
N=(fs/2)*linspace(-1,1,length(x));
stem(N,fftshift(abs(fft(new_x))));
 
title('Sampled signal at frequency-domain');
xlabel('frequency'), ylabel('amplitude');
grid on;
end

Sampling of a Sinusoidal Signal and finding the DTFT [edited]

The question was :

Write a MATLAB function, x = SinSamples(A, w, θ, d, ws), that generates a discrete-time sinusoidal signal x[n] obtained by sampling x(t), (Assume that x(t) is a sinusoidal segment with amplitude A, frequency ω, phase θ and duration ), with sampling frequency ω_s.

function SinSamples(A,w,teta,d,w_s)
 
clc;
fprintf(' This program will read and sample a sin signal.\n The parameters are amplitude A , frequency w , phase "teta" and d for duration. \n This program was written for the Ele 409 DSP Lab course. \n By = John Roach \n\n');
 
pause(2);
 
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(2,1,1);
plot(t,x,'r');
hold on
stem(t1,x1);
title('The sampled and original signal');
xlabel('frequency'), ylabel('amplitude');
grid on;
hold on
 
subplot(2,1,2)
 
N=(fs/2)*linspace(-1,1,length(x1));
stem(N,fftshift(abs(fft(x1))));
title('Sampled signal at frequency-domain');
xlabel('frequency'), ylabel('amplitude');
grid on;

For sample code ;

SinSamples(1,2*pi*1000,pi/6,0.002,2*pi*16000)

The output is;

This program will read sample a sin signal. The parameters are amplitude A , frequency w , phase "teta" and d for duration.  This program was written for the Ele 409 DSP Lab course.  By = John Roach

and;

Amplitude Modulation of a cosine message

Hi there! Have some new code based on Matlab. It was actually a Computer Exercise for my Telecom course. It is an example of Amplitude Modulation of a Cos based message. The code is easy enough. If you have any questions please ask. Hope you enjoy it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function am_modulation
 
N=2000;
F=(-N/2:N/2-1)/N;
 
user_entry1 = input('Please enter carrier freq [Press enter for default fc=0.25] :');
if isempty(user_entry1)
    fc=0.25;
else
    fc=user_entry1;
end
 
user_entry4 = input('Please enter amplitude of carrier wave [Press enter for default Ac=1] :');
if isempty(user_entry4)
    Ac=1;
else
    Ac=user_entry4;
end
 
n=0:1:256;
 
c=Ac*cos(2*pi*n*fc);
subplot(2,2,1)
plot(F, fftshift(abs(fft(c,N))));
title('Carrier Wave at frequency-domain');
xlabel('frequency'), ylabel('amplitude');
grid on;
 
 
 
user_entry2 = input('Please enter message freq [Press enter for default fm=0.01] :');
if isempty(user_entry2)
    fm=0.01;
else
    fm=user_entry2;
end
 
user_entry5 = input('Please enter message amplitude [Press enter for default Am=1] :');
if isempty(user_entry5)
    Am=1;
else
    Am=user_entry5;
end
 
m=Am*cos(2*pi*fm*n);
subplot(2,2,2)
plot(F, fftshift(abs(fft(m,N))));
title('Message at frequency-domain');
xlabel('frequency'), ylabel('amplitude');
grid on;
 
 
user_entry3 = input('Please enter amplitude sensitivity cnst. [Press enter for default ka=0.5] :');
if isempty(user_entry3)
    ka=0.5;
else
    ka=user_entry3;
end
 
s=(1+m.*ka).*c;
 
subplot(2,2,[3 4])
plot(F,fftshift(abs(fft(s,N))));
title('AM waveform at frequency-domain');
xlabel('frequency'), ylabel('amplitude');
grid on;

Here is an example result of the code ;

example_run.jpg

Enjoy & peace!

AM and DSB-SC Modulation and Demodulation of a Periodic Square Wave (Matlab)

We once had a homework way back when decided to share it. I edited this script which is in Matlab. I hope you like it. It simply is an analysis of AM and DSB-SC Modulation and Demodulation of a Periodic Square Wave.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%Amplitude Modulation with a periodic sqare wave and its spectrum analysis
%Show the time domain and frquency domain representation of DSB-AM and
%DSB-SC modulations
%By : John Roach – 6 March 2009
%visit my site at http://johnroach.info
N = 1024; %N point FFT N>fc to avoid freq domain aliasing
fs = 4096; % Sample frequency
t = (0:N-1)/fs;
fc = 600; %Carrier Frequency
fm2 = 80; %Message Frequency
Ec = 20; %Carrier Amplitude
Em2 = 5; %Messagae Amplitude
% Try changing the message and carrier amplitudes to see the effect in
% DSB-AM modulation
%———Double SideBand Full Carrier Modulation (DSB-FC(AM))
A = Ec + Em2*square(2*pi*fm2*t);%Envelope/eliminate the carrier amplitude
m = A.*sin(2*pi*fc*t); %to convert DSB-AM to DSB-SC
Mf = 2/N*abs(fft(m,N));
f = fs * (0 : N/2) / N;%Since the fft result is symmetrical, only the
%positive half is sufficient for spectral representation
close all;
figure(’Name’,'Time/Fequency domain representations of DSB-AM signals’);
subplot(2,1,1); %Time domain plot
plot(t(1:N/2),m(1:N/2),t(1:N/2),A(1:N/2),’r',t(1:N/2),-A(1:N/2),’r');
title(’Time Domain Representation’);
xlabel(’Time’); ylabel(’Modulated signal’);
subplot(2,1,2); %Frequency Domain Plot
plot(f(1:256),Mf(1:256));
title(’Frequency Domain Representation’);
xlabel(’Frequency (Hz)); ylabel(’Spectral Magnitude’);
%———-Double SideBand Suppressed Carrier DSB-SC———-
A = Em2*square(2*pi*fm2*t) ; %Envelope/eliminate the carrier amplitude
m = A.*sin(2*pi*fc*t); %to convert DSB-AM to DSB-SC
Mf = 2/N*abs(fft(m,N));
figure(’Name’,'Time/Fequency domain representations of DSB-SC signals’);
subplot(2,1,1); %Time domain plot
plot(t(1:N/2),m(1:N/2),t(1:N/2),A(1:N/2),’r',t(1:N/2),-A(1:N/2),’r');
title(’Time Domain Representation’);
xlabel(’Time’); ylabel(’Modulated signal’);
subplot(2,1,2); %Frequency Domain Plot
plot(f(1:256),Mf(1:256));
title(’Frequency Domain Representation’);
xlabel(’Frequency (Hz)); ylabel(’Spectral Magnitude’);
text(15,60,’Carrier’);
%——————————————————————–