import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import lfilter
# Parameters
fs = 100 # Sampling frequency
t = np.arange(0, 1, 1/fs) # Time vector
Ain = np.sin(2 * np.pi * 5 * t) # Input signal: 5 Hz sine wave
K = 10 # Gain of the integrator
# Simulate integrator output
integrator_output = np.cumsum(Ain) / fs * K
# Simulate quantizer output (adding quantization noise Q)
Q = np.random.normal(0, 0.1, len(t)) # Quantization noise
quantizer_output = integrator_output + Q
# Feedback signal
feedback_signal = Ain
# Output DOUT
DOUT = quantizer_output / K + feedback_signal
# Low-pass filter design
b, a = [1], [1, 0.5] # Simple low-pass filter coefficients
DOUT_filtered = lfilter(b, a, DOUT)
# Plotting
plt.figure(figsize=(14, 8))
plt.subplot(4, 1, 1)
plt.plot(t, Ain, label='Input Signal $A_{in}$')
plt.title('Input Signal $A_{in}$')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid()
plt.subplot(4, 1, 2)
plt.plot(t, integrator_output, label='Integrator Output $K$')
plt.title('Integrator Output $K$')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid()
plt.subplot(4, 1, 3)
plt.plot(t, quantizer_output, label='Quantizer Output $Q$')
plt.title('Quantizer Output $Q$')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid()
plt.subplot(4, 1, 4)
plt.plot(t, DOUT, label='Output $D_{out}$')
plt.plot(t, DOUT_filtered, label='Filtered $D_{out}$', linestyle='dashed')
plt.title('Output $D_{out}$ and Filtered $D_{out}$')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()
import math
import numpy as np
import matplotlib.pyplot as plt
# Delta-Sigma modulator function
def delSig1st(x):
v0 = 0
y = 0
z = []
for i in range(len(x)):
u = x[i] - y
v = u + v0
v0 = v
if v >= 0:
y = 1
else:
y = -1
z.append(y)
return z
# Signal generation
amp = 0.7
n = 256
omega = 2 * math.pi / 128
x = np.arange(n)
y = amp * np.sin(omega * x)
# Modulation and spectrum calculation
z = delSig1st(y)
Fy = np.fft.fft(y)
spectY = np.abs(Fy)
spy = spectY[0: n//2]
spx = x[0: n//2]
Fz = np.fft.fft(z)
spectZ = np.abs(Fz)
spz = spectZ[0: n//2]
# Plotting and displaying
fig, axs = plt.subplots(2, 1, figsize=(10, 8))
# Plot time-domain signals
axs[0].plot(x, y, label='Input Signal')
axs[0].plot(x, z, label='Modulated Signal')
axs[0].set_title('Time Domain Signals')
axs[0].legend()
axs[0].grid()
# Plot frequency-domain signals
axs[1].plot(spx, spy, label='Input Signal Spectrum')
axs[1].plot(spx, spz, label='Modulated Signal Spectrum')
axs[1].set_title('Frequency Domain Signals')
axs[1].legend()
axs[1].grid()
plt.tight_layout()
plt.show()
def delSigNth(inp, z, N=1):
dither = 0.0
v = [0] * N # Initialize N accumulators
y = 0
for i in range(len(inp)):
u = inp[i] - y
v[0] = u + v[0]
for n in range(1, N):
v[n] = v[n-1] + v[n]
if v[-1] >= 0:
y = 1
else:
y = -1
z[i] = y + dither * random.random()
# Initialize parameters
nn = 1024 * (2 ** 8) # Number of samples
amp = 0.7 # Amplitude
frq = 100.2 # Frequency
smfrq = 44100 # Sampling frequency
repn = 1 # Number of repetitions
N = 1 # Order of Delta-Sigma Modulator
# Prepare data arrays
x = np.arange(nn)
ydat = np.zeros(nn)
z = np.zeros(nn)
sumdat = np.zeros(nn, dtype=complex)
logspect = np.zeros(nn)
# Generate Blackman window
w_black = np.blackman(nn)
# Generate signal and apply Nth-order delta-sigma modulation
for _ in range(repn):
genSignal(amp, frq, smfrq, x, ydat)
delSigNth(ydat, z, N)
# Apply Blackman window
z = z * w_black
# Compute FFT and accumulate results
sumdat += np.fft.fft(z) / repn
# Frequency axis
freq_axis = np.fft.fftfreq(nn, 1/smfrq)
half_nn = nn // 2 # Half the number of samples for positive frequencies
# Calculate magnitude spectrum in dB
spectZ = np.abs(sumdat) / nn
logspect = 20 * np.log10(spectZ + 1e-100) # Avoid log(0) by adding a small number
# Plot the spectrum
plt.plot(freq_axis[:half_nn], logspect[:half_nn])
plt.xscale("log")
plt.ylim(-120, 0)
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude (dB)")
plt.title(f"Frequency Spectrum of {N}-order Delta-Sigma Modulated Signal")
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Parameters
VREF = 2.0 # Reference voltage
num_samples = 1000 # Number of samples
frequency = 1.0 # Frequency of the sine wave
sampling_rate = 1000 # Sampling rate
t = np.arange(num_samples) / sampling_rate # Time vector
# Generate a sine wave as input voltage
VIN = 0.5 * np.sin(2 * np.pi * frequency * t) + 1.0 # Sine wave centered at VREF/2
# Delta-Sigma modulator simulation
def delta_sigma_modulator(vin, vref):
integrator = 0
bitstream = []
integrator_values = []
for sample in vin:
comparator = 1 if integrator > 0 else 0
integrator += sample - comparator * vref
bitstream.append(comparator)
integrator_values.append(integrator)
return bitstream, integrator_values
# Simulate the Delta-Sigma modulator
bitstream, integrator_values = delta_sigma_modulator(VIN, VREF)
# Plot the input sine wave
plt.figure(figsize=(12, 4))
plt.plot(t, VIN)
plt.title('Input Sine Wave')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude [V]')
plt.grid(True)
plt.show()
# Plot the bitstream
plt.figure(figsize=(12, 4))
plt.step(t, bitstream, where='mid')
plt.ylim(-0.1, 1.1)
plt.title('Delta-Sigma Modulator Bitstream')
plt.xlabel('Sample Number')
plt.ylabel('Bit Value')
plt.grid(True)
plt.show()
# Plot the noise shaping (integrator values)
plt.figure(figsize=(12, 4))
plt.plot(t, integrator_values)
plt.title('Noise Shaping (Integrator Values)')
plt.xlabel('Time [s]')
plt.ylabel('Integrator Value')
plt.grid(True)
plt.show()
# Digital filter (simple moving average)
def digital_filter(bitstream, window_size):
filtered_output = np.convolve(bitstream, np.ones(window_size) / window_size, mode='valid')
return filtered_output
# Apply digital filter
window_size = 100
filtered_output = digital_filter(bitstream, window_size)
# Plot the filtered output
plt.figure(figsize=(12, 4))
plt.plot(filtered_output, marker='o')
plt.title('Filtered Output')
plt.xlabel('Sample Number')
plt.ylabel('Filtered Value')
plt.grid(True)
plt.show()
# Calculate the final ADC output
adc_output = np.mean(filtered_output) * VREF
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq
from scipy.signal import butter, filtfilt
# パラメータ
n_bits = 1 # 量子化ビット数
n_points = 1000 # サンプル数
input_frequency = 1 # 入力信号の周波数(Hz)
output_range = 60000 # 出力範囲(nT)
sampling_rate = 100 # サンプリング周波数(Hz)
osr = 64 # オーバーサンプリング比
# サイン波入力信号の生成
time = np.arange(n_points) / sampling_rate
input_signal = np.sin(2 * np.pi * input_frequency * time) * output_range / 2
# Delta-Sigma変調
def delta_sigma_modulation(input_signal, output_range, n_points, n_bits):
quantizer_output = [] # 量子化出力を格納するリスト
integrator = 0 # 積分器の初期値
integrator_output = [] # 積分器出力を格納するリスト
for i in range(n_points): # サンプル数分ループ
integrator += input_signal[i] # 積分器に入力信号を加算
integrator_output.append(integrator) # 積分器の出力を保存
if integrator >= 0: # 積分器の出力が0以上の場合
quantizer_output.append(output_range) # 出力範囲の正の値を量子化出力に追加
integrator -= output_range # 積分器から出力範囲の値を減算
else: # 積分器の出力が0未満の場合
quantizer_output.append(-output_range) # 出力範囲の負の値を量子化出力に追加
integrator += output_range # 積分器に出力範囲の値を加算
return integrator_output, quantizer_output # 積分器出力と量子化出力を返す
# Delta-Sigma変調信号の生成
integrator_output, quantizer_output = delta_sigma_modulation(input_signal, output_range, n_points, n_bits)
# ローパスフィルタの設計
def low_pass_filter(data, cutoff, fs, order=5):
nyquist = 0.5 * fs # ナイキスト周波数を計算
normal_cutoff = cutoff / nyquist # 正規化カットオフ周波数を計算
b, a = butter(order, normal_cutoff, btype='low', analog=False) # Butterworthフィルタの設計
y = filtfilt(b, a, data) # フィルタを適用
return y # フィルタ適用後のデータを返す
# 量子化出力にローパスフィルタを適用
filtered_output = low_pass_filter(quantizer_output, cutoff=5, fs=sampling_rate)
# プロット
plt.figure(figsize=(12, 18))
# 入力信号のプロット
plt.subplot(4, 1, 1)
plt.plot(time, input_signal, label='Input Sine Wave Signal')
plt.title('Input Signal')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
# 積分器出力のプロット
plt.subplot(4, 1, 2)
plt.plot(time, integrator_output, label='Integrator Output')
plt.title('Integrator Output')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
# 量子化出力のプロット
plt.subplot(4, 1, 3)
plt.plot(time, quantizer_output, label='Quantized Output (Delta-Sigma Modulated)')
plt.title('Quantized Output')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
# フィルタ後出力のプロット
plt.subplot(4, 1, 4)
plt.plot(time, filtered_output, label='Filtered Output')
plt.title('Filtered Output')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Define the number of clock cycles
n_cycles = 100
# Generate a sample input signal u(n)
u = np.sin(np.linspace(0, 4 * np.pi, n_cycles))
# Initialize y(n) and v(n)
y = np.zeros(n_cycles)
v = np.zeros(n_cycles)
# Define the DSM equations
for n in range(1, n_cycles):
y[n] = u[n] + y[n-1] - v[n-1]
v[n] = np.round(y[n]) # Quantizer (simple rounding)
# Plot the signals
plt.figure(figsize=(12, 6))
plt.plot(u, label='u(n)', marker='o', linestyle='--')
plt.plot(y, label='y(n)', marker='x', linestyle='-.')
plt.plot(v, label='v(n)', marker='s', linestyle='-')
plt.xlabel('Clock Cycles (n)')
plt.ylabel('Amplitude')
plt.title('Delta-Sigma Modulation: Input, Intermediate, and Output Signals')
plt.legend()
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter
# パラメータ設定
fs = 1000 # サンプリング周波数 (Hz)
f = 5 # 入力サイン波の周波数 (Hz)
T = 1 # シミュレーション時間 (秒)
N = fs * T # サンプル数
# 時間軸の生成
t = np.linspace(0, T, N, endpoint=False)
# 入力信号(サイン波)の生成
input_signal = np.sin(2 * np.pi * f * t)
# デルタシグマ変調器の初期化
integrator = 0
quantizer_output = np.zeros(N)
feedback_signal = np.zeros(N)
# デルタシグマ変調器のシミュレーション
for i in range(N):
integrator += input_signal[i] - feedback_signal[i-1]
if integrator > 0:
quantizer_output[i] = 1
else:
quantizer_output[i] = -1
feedback_signal[i] = quantizer_output[i]
# ローパスフィルタの設計
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
# ローパスフィルタの適用
cutoff_frequency = 10 # カットオフ周波数 (Hz)
filtered_signal = lowpass_filter(quantizer_output, cutoff_frequency, fs)
# プロット
plt.figure(figsize=(12, 12))
# 入力信号のプロット
plt.subplot(4, 1, 1)
plt.plot(t, input_signal, label='Analog Input Signal (Sine Wave)')
plt.title('Analog Input Signal (Sine Wave)')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
# 積分器出力のプロット
plt.subplot(4, 1, 2)
plt.plot(t, np.cumsum(input_signal - feedback_signal), label='Integrator Output')
plt.title('Integrator Output')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
# デルタシグマ変調出力のプロット
plt.subplot(4, 1, 3)
plt.plot(t, quantizer_output, label='Delta-Sigma Modulated Signal')
plt.title('Delta-Sigma Modulated Signal')
plt.xlabel('Time [s]')
plt.ylabel('Output')
plt.grid(True)
# フィルタ後の信号のプロット
plt.subplot(4, 1, 4)
plt.plot(t, filtered_signal, label='Filtered Signal (Analog Output)')
plt.title('Filtered Signal (Analog Output)')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter
# Parameters
fs = 1000 # Sampling frequency
f = 5 # Frequency of the input signal
t = np.arange(0, 1, 1/fs) # Time vector
input_signal = 0.5 * np.sin(2 * np.pi * f * t) # Input signal
# Delta-Sigma Modulation
integrator_output = np.zeros(len(t))
quantizer_output = np.zeros(len(t))
feedback_signal = np.zeros(len(t))
for i in range(1, len(t)):
integrator_output[i] = integrator_output[i-1] + input_signal[i] - feedback_signal[i-1]
quantizer_output[i] = 1 if integrator_output[i] >= 0 else -1
feedback_signal[i] = quantizer_output[i]
# Low-pass filter design
def butter_lowpass(cutoff, fs, order=5):
nyquist = 0.5 * fs
normal_cutoff = cutoff / nyquist
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
# Apply low-pass filter to integrator output
cutoff_frequency = 10 # Cutoff frequency of the LPF
filtered_output = lowpass_filter(integrator_output, cutoff_frequency, fs)
# Plotting
plt.figure(figsize=(12, 10))
plt.subplot(4, 1, 1)
plt.plot(t, input_signal, label='Input Signal')
plt.title('Delta-Sigma Modulation')
plt.legend()
plt.grid()
plt.subplot(4, 1, 2)
plt.plot(t, integrator_output, label='Integrator Output')
plt.legend()
plt.grid()
plt.subplot(4, 1, 3)
plt.plot(t, quantizer_output, label='Quantizer Output')
plt.legend()
plt.grid()
plt.subplot(4, 1, 4)
plt.plot(t, filtered_output, label='Filtered Output (LPF)')
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Function to compute the noise shaping characteristic
def noise_shaping(order, f):
z = np.exp(2j * np.pi * f)
H = (1 - z**-1)**order
return np.abs(H)**2
# Frequency range
frequencies = np.linspace(0, 0.5, 1000) # Normalized frequency (0 to 0.5)
# Plot noise shaping characteristics for different orders
plt.figure(figsize=(10, 6))
for order in range(1, 8):
plt.plot(frequencies, 10 * np.log10(noise_shaping(order, frequencies)), label=f'Order {order}')
plt.xlabel('Normalized Frequency (f)')
plt.ylabel('Noise Power (dB)')
plt.title('Noise Shaping Characteristics of ΔΣ Modulators')
plt.legend()
plt.grid(True)
plt.ylim(-100, 60)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Parameters
fs = 1000 # Sampling frequency
f = 10 # Frequency of the input sine wave
T = 1 # Duration of the signal in seconds
# Time vector
t = np.linspace(0.0, T, T*fs, endpoint=False)
# Input sine wave (A)
input_signal = np.sin(2*np.pi*f*t)
# Delta-Sigma Modulation (B) - Assuming 5-bit quantization
quantized_signal = np.round(input_signal * 16) / 16 # 5-bit quantization
# Plotting
plt.figure(figsize=(10, 6))
plt.subplot(3, 1, 1)
plt.plot(t, input_signal, label='Input Sine Wave')
plt.title('Input Sine Wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
plt.subplot(3, 1, 2)
plt.plot(t, quantized_signal, label='5-bit DSM Output')
plt.title('5-bit DSM Output')
plt.xlabel('Time')
plt.ylabel('Quantized Amplitude')
plt.grid(True)
plt.legend()
plt.subplot(3, 1, 3)
plt.plot(t, input_signal - quantized_signal, label='Difference (A - B)')
plt.title('Difference between Input and DSM Output')
plt.xlabel('Time')
plt.ylabel('Amplitude Difference')
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy import fft, signal
# サンプリング周波数とシミュレーション時間
fs = 1000 # サンプリング周波数 (Hz)
T = 1.0 # シミュレーション時間 (秒)
t = np.arange(0, T, 1/fs) # 時間ベクトル
# 入力信号 (例としてsin波を使用)
input_signal = np.sin(2 * np.pi * t)
# 量子化 (6ビット)
quantized_signal = np.round(input_signal * (2**5)) / (2**5)
# 量子化ノイズ
quantization_noise = input_signal - quantized_signal
# プロット
plt.figure(figsize=(12, 10))
# 入力信号、量子化された信号、量子化ノイズ
plt.subplot(3, 1, 1)
plt.plot(t, input_signal, label='Input Signal')
plt.plot(t, quantized_signal, label='Quantized Signal')
plt.plot(t, quantization_noise, label='Quantization Noise')
plt.title('Input Signal, Quantized Signal, Quantization Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.legend()
# 量子化ノイズの周波数成分
plt.subplot(3, 2, 3)
freqs = fft.fftfreq(len(t), 1/fs)
fft_vals = fft.fft(quantization_noise)
plt.semilogy(freqs[:len(freqs)//2], np.abs(fft_vals[:len(freqs)//2]))
plt.title('Quantization Noise Frequency Components')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
# ハイパスフィルタ処理
cutoff_hz = 50 # カットオフ周波数 (Hz)
b, a = signal.butter(4, cutoff_hz/(fs/2), 'high')
filtered_noise_hp = signal.lfilter(b, a, quantization_noise)
# ハイパスフィルタ処理後の周波数成分
plt.subplot(3, 2, 4)
fft_vals_hp = fft.fft(filtered_noise_hp)
plt.semilogy(freqs[:len(freqs)//2], np.abs(fft_vals_hp[:len(freqs)//2]))
plt.title('Highpass Filtered Quantization Noise Frequency Components')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
# バンドパスフィルタ処理
bandpass_range = (20, 200) # バンドパスの周波数帯域 (Hz)
b, a = signal.butter(4, [bandpass_range[0]/(fs/2), bandpass_range[1]/(fs/2)], 'bandpass')
filtered_noise_bp = signal.lfilter(b, a, quantization_noise)
# バンドパスフィルタ処理後の周波数成分
plt.subplot(3, 2, 5)
fft_vals_bp = fft.fft(filtered_noise_bp)
plt.semilogy(freqs[:len(freqs)//2], np.abs(fft_vals_bp[:len(freqs)//2]))
plt.title('Bandpass Filtered Quantization Noise Frequency Components')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# Parameters
fs = 1000 # Sampling frequency
T = 1 # Measurement time
t = np.linspace(0, T, int(fs * T), endpoint=False) # Time axis
# Input sine wave (assuming a simple sine wave for demonstration)
input_signal = np.sin(2 * np.pi * t)
# Delta-Sigma modulation quantization (simplified to binary output here)
quantized_signal = np.zeros_like(input_signal) # Initialize quantized signal
# Delta-Sigma modulator initial values
sigma1 = 0
sigma2 = 0
# Main loop: Delta-Sigma modulation
for i in range(len(input_signal)):
# Quantizer
if sigma2 > 0:
quantized_signal[i] = 1
else:
quantized_signal[i] = 0
# Update equations
sigma1 = sigma1 + input_signal[i] - quantized_signal[i]
sigma2 = sigma2 + sigma1 - quantized_signal[i]
# Designing a low-pass filter
order = 4
cutoff_freq = 10 # Hz
b, a = signal.butter(order, cutoff_freq / (fs / 2), 'low')
# Applying the filter to the quantized signal
filtered_signal = signal.filtfilt(b, a, quantized_signal)
# Plotting
plt.figure(figsize=(10, 8))
# Plot 1: Input signal
plt.subplot(4, 1, 1)
plt.plot(t, input_signal, label='Input Signal $x(t) = \sin(2\pi t)$')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.legend()
# Plot 2: Quantized signal (binary output)
plt.subplot(4, 1, 2)
plt.step(t, quantized_signal, label='Quantized Signal $y[n]$')
plt.xlabel('Time [s]')
plt.ylabel('Binary Output')
plt.legend()
# Plot 3: Filtered signal
plt.subplot(4, 1, 3)
plt.plot(t, filtered_signal, label='Filtered Signal')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.legend()
# Plot 4: FFT of the quantized signal
plt.subplot(4, 1, 4)
freq = np.fft.fftfreq(len(t), d=1/fs)
fft_quantized = np.fft.fft(quantized_signal)
plt.plot(freq, np.abs(fft_quantized), label='FFT of Quantized Signal')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Magnitude')
plt.legend()
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Parameters
fs = 1000 # Sampling frequency (Hz)
T = 1 # Duration of sampling (seconds)
f_analog = 10 # Analog sine wave frequency (Hz)
quantization_bits = 6 # Number of bits for quantization
# Time vector for analog signal
t_analog = np.linspace(0, T, fs*T, endpoint=False)
# Analog sine wave signal
analog_signal = np.sin(2 * np.pi * f_analog * t_analog)
# Sampling (creating digital signal)
digital_signal = analog_signal.copy()
# Digital filter (1/(1-z^(-1)))
filtered_signal = np.zeros_like(digital_signal)
for i in range(1, len(filtered_signal)):
filtered_signal[i] = digital_signal[i] + digital_signal[i-1]
# Quantization (reduce to quantization_bits)
quantized_signal = np.round(filtered_signal * (2**(quantization_bits-1))) / (2**(quantization_bits-1))
# Feedback filter (z^(-1))
feedback_signal = np.zeros_like(quantized_signal)
for i in range(len(feedback_signal)-1):
feedback_signal[i+1] = quantized_signal[i]
# Plotting
plt.figure(figsize=(10, 6))
plt.subplot(2, 2, 1)
plt.plot(t_analog, analog_signal)
plt.title('Analog Input Sine Wave')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.subplot(2, 2, 2)
plt.stem(t_analog, digital_signal, use_line_collection=True)
plt.title('Digital Sine Wave (Sampled)')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.subplot(2, 2, 3)
plt.plot(t_analog, filtered_signal)
plt.title('Digital Signal after Filter (1/(1-z^(-1)))')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.subplot(2, 2, 4)
plt.stem(t_analog, quantized_signal, use_line_collection=True)
plt.title('Quantized Digital Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Parameters
t = np.linspace(0, 1, 1000) # Time vector
f_sampling = 1000 # Sampling frequency
bits = 3 # Number of quantization bits
# Generate input signal (original sine wave)
input_signal = np.sin(2 * np.pi * t)
# Quantization process
quantized_signal = np.round(input_signal * (2**bits - 1)) / (2**bits - 1)
# Dithering (adding white noise)
dithered_signal = quantized_signal + np.random.uniform(-0.5/(2**bits), 0.5/(2**bits), size=len(t))
# Filters (example: 1 / (1 - z^(-1)))
filtered_signal = np.zeros_like(t)
for i in range(1, len(t)):
filtered_signal[i] = dithered_signal[i] + dithered_signal[i-1]
# Plotting
plt.figure(figsize=(10, 6))
plt.subplot(411)
plt.plot(t, input_signal, label='Input Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.subplot(412)
plt.plot(t, quantized_signal, label='Quantized Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.subplot(413)
plt.plot(t, dithered_signal, label='Dithered Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.subplot(414)
plt.plot(t, filtered_signal, label='Filtered Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# パラメータ設定
N = 1024 # データ点数
t = np.arange(N)
fs = 1000 # サンプリング周波数 (Hz)
f_signal = 5 # 入力信号の周波数 (Hz)
# 入力信号 u(n) の生成
u = np.sin(2 * np.pi * f_signal * t / fs)
# 量子化誤差 e(n) の生成 (ランダム誤差)
e = np.random.normal(0, 0.1, N)
# 出力信号 v(n) の初期化
v = np.zeros(N)
# 2次デルタシグマ変調器の計算
for n in range(2, N):
v[n] = u[n-1] + e[n] - e[n-1] - (e[n-1] - e[n-2])
# FFT の計算
fft_v = np.fft.fft(v)
fft_v = np.abs(fft_v[:N // 2]) * 2 / N # 振幅を計算し、正規化
freqs = np.fft.fftfreq(N, 1 / fs)[:N // 2] # 周波数軸
# プロット
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(t, u, label='Input Signal u(n)')
plt.plot(t, v, label='Output Signal v(n)', alpha=0.75)
plt.xlabel('Time [samples]')
plt.ylabel('Amplitude')
plt.legend()
plt.title('Input and Output Time Series')
plt.subplot(2, 1, 2)
plt.plot(freqs, fft_v)
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude')
plt.title('FFT Spectrum of Output Signal v(n)')
plt.grid(True)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
# アナログ入力信号の生成
fs = 1000 # サンプリング周波数
f = 5 # 入力信号の周波数
t = np.linspace(0, 1, fs) # 時間軸
analog_signal = 0.5 * np.sin(2 * np.pi * f * t) # 入力アナログ信号
# ΔΣ変調器の動作
for va in analog_signal:
# 減算回路(Δ)の計算
if integrator >= 0:
# 積分器の出力が非負の場合
delta = va - Vref
else:
# 積分器の出力が負の場合
delta = va + Vref
# 積分器の更新
integrator += delta
# 加算回路の出力(1ビットの生成)
if integrator >= 0:
bit = 1
else:
bit = 0
# 出力ビットストリームにビットを追加
output_bits.append(bit)
# 積分器の出力を記録
integrator_outputs.append(integrator)
# ローパスフィルタの適用
def butter_lowpass_filter(data, cutoff, fs, order=5):
nyquist = 0.5 * fs
normal_cutoff = cutoff / nyquist
b, a = butter(order, normal_cutoff, btype='low', analog=False)
y = filtfilt(b, a, data)
return y
cutoff_frequency = 10 # カットオフ周波数
filtered_signal = butter_lowpass_filter(output_bits, cutoff_frequency, fs)
# プロット
fig, axs = plt.subplots(4, 1, figsize=(12, 10), sharex=True)
# アナログ入力信号のプロット
axs[0].plot(t, analog_signal, label='Analog Input Signal')
axs[0].set_title('Analog Input Signal')
axs[0].set_ylabel('Amplitude')
axs[0].legend()
# 積分器の出力のプロット
axs[1].plot(t, integrator_outputs, label='Integrator Output')
axs[1].set_title('Integrator Output')
axs[1].set_ylabel('Amplitude')
axs[1].legend()
# ΔΣ変調ビットストリームのプロット
axs[2].step(t, output_bits, where='mid', label='Delta-Sigma Modulated Bitstream')
axs[2].set_title('Delta-Sigma Modulated Bitstream')
axs[2].set_ylabel('Bit Value')
axs[2].legend()
# ローパスフィルタ後の信号のプロット
axs[3].plot(t, filtered_signal, label='Filtered Signal (Lowpass)')
axs[3].set_title('Filtered Signal (Lowpass)')
axs[3].set_xlabel('Time [s]')
axs[3].set_ylabel('Amplitude')
axs[3].legend()
plt.tight_layout()
plt.show()