Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (150 points)
closed by
import math

import matplotlib.pyplot as plt

import numpy as np

import scipy as sp

from scipy import special


%matplotlib inline


#variable decalration

ib_by_ith1=1.5                         #injection current1 > 1.2

ib_by_ith2=1.6                         #injection current2

ib_by_ith3=1.7                         #injection current3

f = 100.0*10**6                          #frequency (hz)


#calculation

RIN1 = ((ib_by_ith1-1)**-3)/f

RIN2 = ((ib_by_ith2-1)**-3)/f

RIN3 = ((ib_by_ith3-1)**-3)/f

RIN_dB1 = 20*math.log10(RIN1)             #noise to signal power ratio1(dB/Hz)

RIN_dB2 = 20*math.log10(RIN2)             #noise to signal power ratio2(dB/Hz)

RIN_dB3 = 20*math.log10(RIN3)             #noise to signal power ratio3(dB/Hz)


#result

print ("Index guided lasers lies between ",round(RIN_dB1,0),"dB/Hz",round(RIN_dB2,0),"dB/Hz",round(RIN_dB3,1),"dB/Hz")


#plot

f1=1.0*10**8                                   # several hundrede MHz

ib_by_ith = np.arange(0.0, 2.5, 0.1)

RIN12 = ((ib_by_ith-1.0)**-3)/f1

plt.plot(ib_by_ith,20*math.log10(RIN12))

ylabel('RIN(dB/Hz)')

xlabel('Ib/Ith')

title('Plot of relative intensity. The noise level=100MHz ')

plt.show()
closed

3 Answers

0 votes
by (25.7k points)
 
Best answer
You are encountering an error because ib_by_ith is a NumPy array and the math.log10() function expects a scalar value. To resolve this issue, you can use the NumPy log10() function instead, which can handle array operations. Here's the modified code:

import math

import matplotlib.pyplot as plt

import numpy as np

%matplotlib inline

# Variable declaration

ib_by_ith1 = 1.5  # injection current1 > 1.2

ib_by_ith2 = 1.6  # injection current2

ib_by_ith3 = 1.7  # injection current3

f = 100.0 * 10 ** 6  # frequency (hz)

# Calculation

RIN1 = ((ib_by_ith1 - 1) ** -3) / f

RIN2 = ((ib_by_ith2 - 1) ** -3) / f

RIN3 = ((ib_by_ith3 - 1) ** -3) / f

RIN_dB1 = 20 * np.log10(RIN1)  # noise to signal power ratio1(dB/Hz)

RIN_dB2 = 20 * np.log10(RIN2)  # noise to signal power ratio2(dB/Hz)

RIN_dB3 = 20 * np.log10(RIN3)  # noise to signal power ratio3(dB/Hz)

# Result

print("Index guided lasers lies between", round(RIN_dB1, 0), "dB/Hz", round(RIN_dB2, 0), "dB/Hz", round(RIN_dB3, 1),

      "dB/Hz")

# Plot

f1 = 1.0 * 10 ** 8  # several hundred MHz

ib_by_ith = np.arange(0.0, 2.5, 0.1)

RIN12 = ((ib_by_ith - 1.0) ** -3) / f1

plt.plot(ib_by_ith, 20 * np.log10(RIN12))

plt.ylabel('RIN(dB/Hz)')

plt.xlabel('Ib/Ith')

plt.title('Plot of relative intensity. The noise level = 100MHz')

plt.show()

In this modified code, I replaced math.log10() with np.log10() to handle the array operation correctly. This should resolve the error you were encountering.
0 votes
by (15.4k points)
You are encountering an error in the line plt.plot(ib_by_ith, 20*math.log10(RIN12)) because the math.log10() function is unable to handle array operations. To resolve this, you can replace math.log10() with np.log10() from the NumPy library, which can handle array calculations. Here's the revised code:

import math

import matplotlib.pyplot as plt

import numpy as np

%matplotlib inline

# Variable declaration

ib_by_ith1 = 1.5  # injection current1 > 1.2

ib_by_ith2 = 1.6  # injection current2

ib_by_ith3 = 1.7  # injection current3

f = 100.0 * 10 ** 6  # frequency (hz)

# Calculation

RIN1 = ((ib_by_ith1 - 1) ** -3) / f

RIN2 = ((ib_by_ith2 - 1) ** -3) / f

RIN3 = ((ib_by_ith3 - 1) ** -3) / f

RIN_dB1 = 20 * np.log10(RIN1)  # noise to signal power ratio1(dB/Hz)

RIN_dB2 = 20 * np.log10(RIN2)  # noise to signal power ratio2(dB/Hz)

RIN_dB3 = 20 * np.log10(RIN3)  # noise to signal power ratio3(dB/Hz)

# Result

print("Index guided lasers lie between", round(RIN_dB1, 0), "dB/Hz,", round(RIN_dB2, 0), "dB/Hz, and",

      round(RIN_dB3, 1), "dB/Hz")

# Plot

f1 = 1.0 * 10 ** 8  # several hundred MHz

ib_by_ith = np.arange(0.0, 2.5, 0.1)

RIN12 = ((ib_by_ith - 1.0) ** -3) / f1

plt.plot(ib_by_ith, 20 * np.log10(RIN12))

plt.ylabel('RIN(dB/Hz)')

plt.xlabel('Ib/Ith')

plt.title('Plot of relative intensity. The noise level = 100MHz')

plt.show()

By utilizing np.log10() instead of math.log10(), the array calculations will be handled correctly, resolving the error you encountered.
0 votes
by (19k points)
import matplotlib.pyplot as plt

import numpy as np

# Variable declaration

ib_by_ith = np.arange(0.0, 2.5, 0.1)

f = 100.0 * 10 ** 6  # frequency (Hz)

f1 = 1.0 * 10 ** 8  # several hundred MHz

# Calculation

RIN12 = ((ib_by_ith - 1.0) ** -3) / f1

RIN_dB = 20 * np.log10(RIN12)

# Result

print("Index guided lasers lie between", round(min(RIN_dB), 0), "dB/Hz and", round(max(RIN_dB), 1), "dB/Hz")

# Plot

plt.plot(ib_by_ith, RIN_dB)

plt.ylabel('RIN(dB/Hz)')

plt.xlabel('Ib/Ith')

plt.title('Plot of relative intensity. The noise level = 100MHz')

plt.show()

This version simplifies the code by removing unnecessary variables and calculations. It calculates the RIN values directly using the ib_by_ith array and performs logarithmic operations using np.log10(). The result is printed by finding the minimum and maximum values of RIN_dB. The plot is generated using the simplified variables and displayed using plt.show().

Browse Categories

...