0% found this document useful (0 votes)
8 views2 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import numpy as np

import matplotlib.pyplot as plt

def quan(dc_shifted,level):

range=max(dc_shifted)-min(dc_shifted)

step_size=range/(2**level-1)

quantized_wave=np.round(dc_shifted/step_size)*step_size

return quantized_wave

def decimal_to_binary(decimal_value,num_bits):

binary_str=bin(int(decimal_value))[2:].zfill(num_bits)

return[int(bit)for bit in binary_str]

def calculate_power(signal):

return np.mean(np.square(signal))

sampling_rate=1000

duration=1

f=10

dc_offset=2

t=np.linspace(0,duration,sampling_rate)

original_wave=np.sin(2*np.pi*f*t)

dc_shifted=original_wave+dc_offset

plt.subplot(3,1,1)

plt.plot(t,original_wave)

plt.title("original waveform")

plt.subplot(3,1,2)

plt.plot(t,dc_shifted)

plt.title("DC shifted waveform")

plt.subplot(3,1,3)

plt.plot(t,quan(dc_shifted,8))

plt.tight_layout()

plt.show()

quantized_wave=quan(dc_shifted,8)

max_bits=len(bin(int(max(quantized_wave))))-2

binary_encoded_wave=np.array([decimal_to_binary(value,max_bits)for value in quantized_wave])

print("binary encoded wave=",binary_encoded_wave)


signal_power=calculate_power(dc_shifted)

levels=range(2,17)

SNRs=[]

for level in levels:

quantized_wave=quan(dc_shifted,level)

quantization_error=dc_shifted-quantized_wave

noise_power=calculate_power(quantization_error)

SNR=10*np.log10(signal_power/noise_power)

SNRs.append(SNR)

plt.plot(levels,SNRs)

plt.xlabel('Number of bits per symbol')

plt.ylabel('SNR(db)')

plt.title('SNR versus no of bits per symbol')

plt.show()

frequency = 1

amplitude = 1

dc_offset = 2

max_bits = 3

L = 2**max_bits

sampling_rate = 179

duration = 2

t = np.arange(0, duration, 1/sampling_rate)

sine_wave = amplitude * np.sin(2 * np.pi * frequency * t) + dc_offset

quantized_sine_wave = np.round((sine_wave - dc_offset) / (2 * amplitude / (L - 1))) * (2 * amplitude / (L - 1)) + dc_offset

step_size = (2 * amplitude) / (L - 1)

plt.plot(t, sine_wave, label='Original Sine Wave')

plt.plot(t, quantized_sine_wave, label=f'Quantized Sine Wave (L={L})')

plt.title('Quantization of Sine Wave')

plt.xlabel('Time (seconds)')

plt.ylabel('Amplitude')

plt.legend()

You might also like