| 12
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 
 | clear;
 close all;
 clc;
 
 f_in = 0.61*10^3;
 f_s = 10.24*10^3;
 M = 1024;
 n = (0:1023)/f_s;
 
 V_REF = 5;
 
 V_in = V_REF * sin(2*pi*f_in*n);
 
 
 
 N = 6;
 V_in = [V_in,zeros(1,N-1)];
 
 decision = zeros(1,N);
 DAC_output = zeros(1,N);
 
 comp_offset1 = -V_REF/4 + (V_REF/2)*rand(1,1);
 comp_offset2 = -V_REF/4 + (V_REF/2)*rand(1,1);
 
 ADC_output = zeros(1,1024);
 
 
 for i = 1:1024+N-1
 
 for j = N:-1:1
 if i >= j
 
 if j == 1
 input = V_in(i);
 else
 input = DAC_output(j-1);
 end
 
 if input >= V_REF/4 + comp_offset1
 decision(j) = 1;
 DAC_output(j) = 2*input - V_REF;
 elseif input < -V_REF/4 + comp_offset2
 decision(j) = 0;
 DAC_output(j) = 2*input + V_REF;
 else
 if j == N
 decision(j) = 0;
 else
 decision(j) = 1/2;
 end
 DAC_output(j) = 2*input;
 end
 
 if i-j+1 <= 1024
 ADC_output(i-j+1) = ADC_output(i-j+1) + 2^(N-j) * decision(j);
 end
 
 else
 disp("The  stage have no data yet");
 end
 end
 
 end
 
 ADC_output_analog = (ADC_output-2^(N-1))*(10/2^N);
 plot(ADC_output_analog- V_in(1:1024));title("error");xlim([1,1024]);grid on;
 
 Y = abs(fft(ADC_output_analog));
 figure(2);
 plot(0:1023,Y);xlabel("k");title("FFT of ADC output");grid on;
 
 transition = -5:10/(2^N):5;
 
 group_count_actual = zeros(1,64);
 group_count_theoritical = zeros(1,64);
 
 g = discretize(ADC_output_analog,transition);
 
 for i = 1:1024
 group_count_actual(g(i)) = group_count_actual(g(i)) + 1;
 end
 
 for i = 1:64
 group_count_theoritical(i) = 1024 * (asin(transition(i+1)/V_REF)-asin(transition(i)/V_REF))/pi;
 end
 
 DNL = zeros(1,64);
 INL = zeros(1,64);
 
 for i = 1:64
 DNL(i) = group_count_actual(i)/group_count_theoritical(i) - 1;
 if i ==1
 INL(1) = DNL(1);
 else
 INL(i) = INL(i-1) + DNL(i);
 end
 end
 
 figure(3);
 subplot(1,2,1);
 plot(DNL);xlim([1,64]);xlabel("n");title("DNL");grid on;
 subplot(1,2,2);
 plot(INL);xlim([1,64]);xlabel("n");title("INL");grid on;
 
 |