-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp7.txt
77 lines (77 loc) · 1.77 KB
/
exp7.txt
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
67
68
69
70
71
72
73
74
75
76
77
clc
clear all
close all
t = -5:0.01:5; %basic time axis
f =2;
w = 2*pi*f;
osr = 250; %can vary
fs1 = w/pi;
fs = fs1*osr;
%% sampling time
ts = -5:(1/fs):5; %sampling times are defined
y = @(t)sin(w.*t); %signal is defined
%% sigma delta quantisation
[u,q] = SDQ(y(ts),ts);
%% reconstruction algorithm
z = 0;
for k = 1:length(ts)
z = z + q(k).*sinc(w.*(t - ts(k)));
end
c = max(y(t))./max(z); %scaling is done as a consequence of oversampling
z = z.*c;
%% figures
figure(1)
subplot(3,1,1)
plot(t,y(t),'linewidth',2)
title('Original sinal')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,2)
plot(ts,q)
title('SDQ signal');
xlabel('Time');
ylabel('Amplitude');
subplot(3,1,3)
plot(t,z,'linewidth',2);
title('Reconstructed signal');
xlabel('Time');
ylabel('Amplitude');
figure(2);
plot(t,y(t),'linewidth',2)
hold on
plot(t,z,'linewidth',2);
title('Original vs Reconstructed');
figure(3);
plot(abs(z - y(t)),'linewidth',2);
title('Error');
figure(4);
subplot(3,1,1);
plot(abs(fftshift(fft(y(t)))));
xlabel('Frequency');
ylabel('Amplitude');
title('Spectrum of original signal');
subplot(3,1,2);
plot(abs(fftshift(fft(q))));
xlabel('Frequency');
ylabel('Amplitude');
title('Spectrum of SDQ');
subplot(3,1,3);
plot(abs(fftshift(fft(z))));
title('Spectrum of recovered signal');
xlabel('Frequency');
ylabel('Amplitude');
%% mse computation
error = immse(z,y(t));
%% function
function [u,q] = SDQ(y,t)
%as per basic equations, models a sigma delta modulator
%% code logic
q = zeros(1,length(t));
u = zeros(1,length(t)); %quantizaton noise/state variable
u(1) = 0.9; %taken 0.9 as in between 0 and 1 for stability (non inclusive)
%recursive equations for SDQ
for k = 2:length(t)
q(k) = sign(u(k-1) + y(k));
u(k) = u(k-1) + y(k) - q(k);
end
end