# 2) Plot Signal With Respect To The Time.: # Imports

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

# imports

import soundfile
import sounddevice
import numpy as np
import matplotlib.pyplot as plt

Backing off send_request(...) for 347.8s (requests.exceptions.ConnectionError


: HTTPSConnectionPool(host='api.segment.io', port=443): Max retries exceeded
with url: /v1/batch (Caused by NewConnectionError('<urllib3.connection.HTTPSC
onnection object at 0x00000199D5D6CCA0>: Failed to establish a new connection
: [Errno 11001] getaddrinfo failed')))

Data
filename = 'chirp.wav'
y, fs = soundfile.read(filename)

1) Find the number of samples (𝑁0), the duration of the signal (𝑇0), and
sampling interval (𝑇)
N = len(y) # number of samples (𝑁0) in the audio file
print(f"The number of samples is {N}")
Ts = 1 / fs # the sampling interval (𝑇)
print(f"The sampling interval is {Ts}")
T = N * Ts # the duration of the signal (𝑇0)
print(f"The duration of the signal is {T}")

The number of samples is 13129


The sampling interval is 0.0001220703125
The duration of the signal is 1.6026611328125

# 2) Plot signal 𝑦 with respect to the time.


plt.figure(figsize=(20,10))
plt.grid()
plt.xlabel('Time (s)')
plt.ylabel('Magnitude of Signal y')
plt.plot(np.arange(0, T, Ts), y)

[<matplotlib.lines.Line2D at 0x199d7a99280>]
3) Compute and plot the DFT of signal 𝑦.
X = np.fft.fft(y) # Compute the DFT of signal 𝑦

freq = np.arange(0, fs, fs/N) # Compute the frequency vector

plt.figure(figsize=(20,10))
plt.grid()
plt.xlabel('Frequency')
plt.ylabel('Magnitude of Signal DFT(y)')
plt.plot(freq, abs(X)) # Plot the magnitude of the DFT

[<matplotlib.lines.Line2D at 0x199d7af52e0>]

Backing off send_request(...) for 1.1s (requests.exceptions.ConnectionError:


HTTPSConnectionPool(host='api.segment.io', port=443): Max retries exceeded wi
th url: /v1/batch (Caused by NewConnectionError('<urllib3.connection.HTTPSCon
nection object at 0x00000199D684B8E0>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed')))
4) Generate the subsampled signal 𝑦1 from signal 𝑦 by subsampling with rate 2.
Find the number of samples (𝑁0), the duration of the signal (𝑇0), and sampling
interval (𝑇).
y1 = y[::2]

N1 = len(y1) # the number of samples


print(f"The number of samples is {N1}")
Ts1 = Ts / 2 # the sampling interval
print(f"The sampling interval is {Ts1}")
T1 = N1 * Ts1 # the duration of the signal
print(f"The duration of the signal is {T1}")

The number of samples is 6565


The sampling interval is 6.103515625e-05
The duration of the signal is 0.40069580078125

5) Plot signal 𝑦1 with respect to time.


plt.figure(figsize=(20,10))
plt.grid()
plt.xlabel('Time')
plt.ylabel('Magnitude of Signal y1')
plt.plot(np.arange(0, T1, Ts1), y1)

[<matplotlib.lines.Line2D at 0x199d7e3c7c0>]
6) Compute and plot DFT of 𝑦1
X1 = np.fft.fft(y1) # Compute the DFT of signal 𝑦1

freq1 = np.arange(0, fs, fs/N) # Compute the frequency vector

# plt.plot(freq1, abs(X1)) # Plot the magnitude of the DFT


plt.figure(figsize=(20,10))
plt.grid()
plt.xlabel('Time')
plt.ylabel('Magnitude of Signal DFT(y1)')
plt.plot(np.arange(0, T1, Ts1), abs(X1)) # Plot the magnitude of the DFT w r
t the time

[<matplotlib.lines.Line2D at 0x199d7ed97f0>]
How has the signal 𝑦1 and its spectrum changed compared to the original
signal?
???

7) To listen to the audio signal, use the command sounddevice.play(x, fs) . Play
both signals and see how the original audio signal has changed after
subsampling.
play1 = sounddevice.play(y, fs)

play2 = sounddevice.play(y1, fs)

8) Change the sampling rate in step (4) to 5 and explain how the audio signal
and its spectrum change.
y2 = y[::5]

N2 = len(y2) # the number of samples


print(f"The number of samples is {N2}")
Ts2 = Ts / 2 # the sampling interval
print(f"The sampling interval is {Ts2}")
T2 = N2 * Ts2 # the duration of the signal
print(f"The duration of the signal is {T2}")
plt.figure(figsize=(20,10))
plt.grid()
plt.xlabel('Time')
plt.ylabel('Magnitude of Signal DFT(y2)')
plt.plot(np.arange(0, T2, Ts2), y2)

The number of samples is 2626


The sampling interval is 6.103515625e-05
The duration of the signal is 0.1602783203125

[<matplotlib.lines.Line2D at 0x199da1a0400>]

Backing off send_request(...) for 1.6s (requests.exceptions.ConnectionError:


HTTPSConnectionPool(host='api.segment.io', port=443): Max retries exceeded wi
th url: /v1/batch (Caused by NewConnectionError('<urllib3.connection.HTTPSCon
nection object at 0x00000199D684B640>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed')))
Explain how the audio signal and its spectrum change.
???

You might also like