1.5ビットパイプライン型ADコンバータのシミュレーション


参考にしたサイト

https://miscellaneous.tokyo/blog/pipeline-adc/

```py

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Number of divisions
n = 64

# Generate input signal (range from -1 to 1)
input_signal = np.linspace(-1, 1, n)

# T/H circuit output (same as input)
th_output1 = input_signal

# ADC output with special thresholds
def adc_output(th_output):
    return np.where(th_output <= -0.25, "00", 
                    np.where(th_output < 0.25, "01", "10"))

adc_output1 = adc_output(th_output1)

# DAC output based on ADC output
def dac_output(adc_output):
    return np.where(adc_output == "00", -0.5, 
                    np.where(adc_output == "01", 0, 0.5))

dac_output1 = dac_output(adc_output1)

# Subtractor output
subtractor_output1 = th_output1 - dac_output1

# 2x amplifier output
amp_output1 = subtractor_output1 * 2

# Second stage
th_output2 = amp_output1
adc_output2 = adc_output(th_output2)
dac_output2 = dac_output(adc_output2)
subtractor_output2 = th_output2 - dac_output2
amp_output2 = subtractor_output2 * 2

# Third stage
th_output3 = amp_output2
adc_output3 = adc_output(th_output3)
dac_output3 = dac_output(adc_output3)
subtractor_output3 = th_output3 - dac_output3
amp_output3 = subtractor_output3 * 2

# Fourth stage
th_output4 = amp_output3
adc_output4 = adc_output(th_output4)
dac_output4 = dac_output(adc_output4)
subtractor_output4 = th_output4 - dac_output4
amp_output4 = subtractor_output4 * 2

# Create a DataFrame to hold all values for easy viewing
df = pd.DataFrame({
    'Input': input_signal,
    'TH Output 1': th_output1,
    'ADC Output 1': adc_output1,
    'DAC Output 1': dac_output1,
    'Subtractor Output 1': subtractor_output1,
    'Amplifier Output 1': amp_output1,
    'TH Output 2': th_output2,
    'ADC Output 2': adc_output2,
    'DAC Output 2': dac_output2,
    'Subtractor Output 2': subtractor_output2,
    'Amplifier Output 2': amp_output2,
    'TH Output 3': th_output3,
    'ADC Output 3': adc_output3,
    'DAC Output 3': dac_output3,
    'Subtractor Output 3': subtractor_output3,
    'Amplifier Output 3': amp_output3,
    'TH Output 4': th_output4,
    'ADC Output 4': adc_output4,
    'DAC Output 4': dac_output4,
    'Subtractor Output 4': subtractor_output4,
    'Amplifier Output 4': amp_output4
})

# Print the DataFrame
print(df)

# Plotting the results
plt.figure(figsize=(12, 16))

plt.subplot(4, 2, 1)
plt.plot(input_signal, th_output1, label='T/H Output 1')
plt.xlabel('Input')
plt.ylabel('T/H Output 1')
plt.legend()

plt.subplot(4, 2, 2)
plt.plot(input_signal, dac_output1, label='DAC Output 1', color='r')
plt.xlabel('Input')
plt.ylabel('DAC Output 1')
plt.legend()

plt.subplot(4, 2, 3)
plt.plot(input_signal, subtractor_output1, label='Subtractor Output 1', color='g')
plt.xlabel('Input')
plt.ylabel('Subtractor Output 1')
plt.legend()

plt.subplot(4, 2, 4)
plt.plot(input_signal, amp_output1, label='Amplifier Output 1', color='m')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 1')
plt.legend()

plt.subplot(4, 2, 5)
plt.plot(input_signal, th_output2, label='T/H Output 2', color='b')
plt.xlabel('Input')
plt.ylabel('T/H Output 2')
plt.legend()

plt.subplot(4, 2, 6)
plt.plot(input_signal, amp_output2, label='Amplifier Output 2', color='c')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 2')
plt.legend()

plt.subplot(4, 2, 7)
plt.plot(input_signal, amp_output3, label='Amplifier Output 3', color='y')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 3')
plt.legend()

plt.subplot(4, 2, 8)
plt.plot(input_signal, amp_output4, label='Amplifier Output 4', color='k')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 4')
plt.legend()

plt.tight_layout()
plt.show()
```

結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3759109/6c4b1acc-41a3-0458-4af5-3239701c4ecd.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3759109/b81c7087-1385-027f-b0d1-d3c375e7bfcd.png)





```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Number of divisions
n = 64

# Generate input signal (range from -1 to 1)
input_signal = np.linspace(-1, 1, n)

# T/H circuit output (same as input)
th_output1 = input_signal

# ADC output with special thresholds
def adc_output(th_output):
    return np.where(th_output <= -0.25, "00", 
                    np.where(th_output < 0.25, "01", "10"))

adc_output1 = adc_output(th_output1)

# DAC output based on ADC output
def dac_output(adc_output):
    return np.where(adc_output == "00", -0.5, 
                    np.where(adc_output == "01", 0, 0.5))

dac_output1 = dac_output(adc_output1)

# Subtractor output
subtractor_output1 = th_output1 - dac_output1

# 2x amplifier output
amp_output1 = subtractor_output1 * 2

# Second stage
th_output2 = amp_output1
adc_output2 = adc_output(th_output2)
dac_output2 = dac_output(adc_output2)
subtractor_output2 = th_output2 - dac_output2
amp_output2 = subtractor_output2 * 2

# Create a DataFrame to hold all values for easy viewing
df = pd.DataFrame({
    'Input': input_signal,
    'TH Output 1': th_output1,
    'ADC Output 1': adc_output1,
    'DAC Output 1': dac_output1,
    'Subtractor Output 1': subtractor_output1,
    'Amplifier Output 1': amp_output1,
    'TH Output 2': th_output2,
    'ADC Output 2': adc_output2,
    'DAC Output 2': dac_output2,
    'Subtractor Output 2': subtractor_output2,
    'Amplifier Output 2': amp_output2
})

# Print the DataFrame
print(df)

# Plotting the results
plt.figure(figsize=(12, 16))

plt.subplot(4, 2, 1)
plt.plot(input_signal, th_output1, label='T/H Output 1')
plt.xlabel('Input')
plt.ylabel('T/H Output 1')
plt.legend()

plt.subplot(4, 2, 2)
plt.plot(input_signal, dac_output1, label='DAC Output 1', color='r')
plt.xlabel('Input')
plt.ylabel('DAC Output 1')
plt.legend()

plt.subplot(4, 2, 3)
plt.plot(input_signal, subtractor_output1, label='Subtractor Output 1', color='g')
plt.xlabel('Input')
plt.ylabel('Subtractor Output 1')
plt.legend()

plt.subplot(4, 2, 4)
plt.plot(input_signal, amp_output1, label='Amplifier Output 1', color='m')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 1')
plt.legend()

plt.subplot(4, 2, 5)
plt.plot(input_signal, th_output2, label='T/H Output 2', color='b')
plt.xlabel('Input')
plt.ylabel('T/H Output 2')
plt.legend()

plt.subplot(4, 2, 6)
plt.plot(input_signal, dac_output2, label='DAC Output 2', color='r')
plt.xlabel('Input')
plt.ylabel('DAC Output 2')
plt.legend()

plt.subplot(4, 2, 7)
plt.plot(input_signal, subtractor_output2, label='Subtractor Output 2', color='g')
plt.xlabel('Input')
plt.ylabel('Subtractor Output 2')
plt.legend()

plt.subplot(4, 2, 8)
plt.plot(input_signal, amp_output2, label='Amplifier Output 2', color='c')
plt.xlabel('Input')
plt.ylabel('Amplifier Output 2')
plt.legend()

plt.tight_layout()
plt.show()
```

結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3759109/781ee489-c2a0-531d-9902-61733089e40d.png)