Embedded LAB

Digital Signal Processing

The purpose of this blog is to demonstrate digital signal processing using Python programming. Generating discrete signals and performing DSP operations such as filtering, compression, and modulation. We'll use Python and the Google Colab environment to study how discrete signals can be generated and signal processing operations can be performed.

Prerequisite

  • Basics of Python language.
  • Python modules like matplotlib, and numpy.
  • Colab Notebook

Basic Discrete Time Signals

We will examine the discrete-time signal equations and attempt to plot and produce signals in Python using the numpy and matplot modules.

  • Discrete Unit Impulse Signal

    The equation is given as

    δ[n]=1 n=0
           0 otherwise
/*--------------code begin--------------------------------*/
# importing libraries
import numpy as np
import matplotlib.pyplot as plt

# Function to plot Impulse signal d(a)
def unit_impulse(a, n):
    delta =[]
    for sample in n:
        if sample == a:
            delta.append(1)
        else:
            delta.append(0)
              
    return delta
  
a = 1  # Enter delay or advance
UL = 4 # x-axis limit
LL = -4
n = np.arange(LL, UL)
d = unit_impulse(a, n)# create array
plt.stem(n, d) 
plt.xlabel('n')
plt.xticks(np.arange(LL, UL, 1))
plt.yticks([0, 1])
plt.ylabel('d[n]')
plt.title('Unit Impulse')
plt.savefig("UnitImpulse.png")
/*---------------code end-------------------------------*/


  • Discrete Unit Step Signal

   The equation is given as

   δ[n]=1 n=0,1,2,...
          0 otherwise
/*--------------code begin--------------------------------*/
# importing libraries
import numpy as np
import matplotlib.pyplot as plt

# Function to plot Unit Step signal d(a)
def unit_impulse(a, n):
    delta =[]
    for sample in n:
        if sample > a:
            delta.append(1)
        else:
            delta.append(0)
              
    return delta
  
a = 0 # Enter delay or advance
UL = 10
LL = -10
n = np.arange(LL, UL)
d = unit_impulse(a, n)# create array
plt.stem(n, d)
plt.xlabel('n')
plt.xticks(np.arange(LL, UL, 1))
plt.yticks([0, 1])
plt.ylabel('d[n]')
plt.title('Unit Impulse d[4]')
plt.savefig("UnitImpulse.jpeg")
/*---------------code end-------------------------------*/

  • Discrete Unit Ramp Signal

    The equation is given as
    
    δ[n]=n n=0,1,2,...
           0 otherwise

/*--------------code begin--------------------------------*/
# importing libraries
import numpy as np
import matplotlib.pyplot as plt
# Function to generate unit ramp signal r(n)
# r(n)= n for n>= 0, r(n)= 0 otherwise
def unit_ramp(n):
    ramp =[]
    for sample in n:
        if sample<0:
            ramp.append(0)
        else:
            ramp.append(sample)
    return ramp
  
UL = 10
LL = -10
n = np.arange(LL, UL, 1)
r = unit_ramp(n)
plt.stem(n, r)
plt.xlabel('n')
plt.xticks(np.arange(LL, UL, 1))
plt.yticks(np.arange(0, UL, 1))
plt.ylabel('r[n]')
plt.title('Unit Ramp')
plt.savefig("UnitRamp.jpeg")
/*---------------code end-------------------------------*/



  • Discrete Time Exponential Signal  

   The equation is given as    

   e[n]=an n= -1.0,-0.9,-0.8...
/*--------------code begin--------------------------------*/
# importing libraries
import numpy as np
import matplotlib.pyplot as plt

# Function to generate exponential signals e**(at)
def exponential(a, n):
    expo =[]
    for sample in n:
        expo.append(np.exp(a * sample))
    return (expo)
         
a = 0.8
UL = 1
LL = -1
n = np.arange(LL, UL, 0.1)
x = exponential(a, n)
plt.stem(n, x)
plt.xlabel('n')
plt.xticks(np.arange(LL, UL, 0.2))
# plt.yticks([0, UL, 1])
plt.ylabel('x[n]')
plt.title('Exponential Signal')
plt.savefig("Exponential.jpeg")
/*---------------code end-------------------------------*/


  • Sin Wave Signal  

   The equation is given as    

   Asin(ωt) 
   
   A:Amplitude
   t:Time
   ω: Frequency

/*--------------code begin--------------------------------*/
import numpy as np
import matplotlib.pyplot as plt 
# Get x values of the sine wave
time = np.arange(0, 20,0.100); # start,stop,step
# Amplitude of the sine wave is sine of a variable like time
amplitude = np.sin(time)
# Plot a sine wave using time and amplitude obtained for the sine wave
plt.stem(time,amplitude)
# Give a title for the sine wave plot
plt.title('Sine wave')
# Give x axis label for the sine wave plot
plt.xlabel('Time')
# Give y axis label for the sine wave plot
plt.ylabel('Amplitude = sin(time)')
plt.grid(True, which='both')
plt.axhline(y=0, color='r')
#Save the figure
plt.savefig("Sine wave.jpeg")
/*---------------code end-------------------------------*/


Please follow the below link to refer the codes in colab environment:
github repository:colab code


Keep Visiting the page for further updates on experiments!!

Comments

Popular Posts