0% found this document useful (0 votes)
111 views

Import Numpy As NP

This document contains Python code to generate and analyze sine wave signals. It creates a sine wave, adds noise, performs a Fourier transform to analyze the frequencies, filters out the noise frequency, and recovers the original sine wave signal. Key steps include generating a sine wave, adding random noise, performing an FFT to identify frequencies, filtering to remove the noise frequency, and an inverse FFT to recover the original signal.

Uploaded by

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

Import Numpy As NP

This document contains Python code to generate and analyze sine wave signals. It creates a sine wave, adds noise, performs a Fourier transform to analyze the frequencies, filters out the noise frequency, and recovers the original sine wave signal. Key steps include generating a sine wave, adding random noise, performing an FFT to identify frequencies, filtering to remove the noise frequency, and an inverse FFT to recover the original signal.

Uploaded by

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

import numpy as np

import wave

import struct

import matplotlib.pyplot as plt

# frequency is the number of times a wave repeats a second

frequency=1000

num_samples=48000

# The sampling rate of the analog to digital convert

sampling_rate=48000.0

amplitude=16000

file= "test.wav"

sine_wave=[np.sin(2*np.pi*frequency*x/sampling_rate) for x in range(num_samples)]

nframes=num_samples

comptype="NONE"

compname="not compressed"

nchannels=1

sampwidth=2

wav_file=wave.open(file, 'w' )
wav_file.setparams((nchannels, sampwidth, int (sampling_rate),nframes, comptype,compname))

for s in sine_wave:

wav_file.writeframes(struct.pack('h', int(s*amplitude)))

frame_rate = 48000.0

infile= "test.wav"

num_samples = 48000

wav_file = wave.open(infile, 'r')

data = wav_file.readframes(num_samples)

wav_file.close()

data = struct.unpack('{n}h'.format(n=num_samples), data)

data = np.array(data)

data_fft= np.fft.fft(data)

# This will give us the frequency we want


frequencies= np.abs(data_fft)

data_fft= np.fft.fft(sine_wave)

abs(data_fft[0])

print("The frequency is {} Hz".format(np.argmax(frequencies)))

plt.subplot(2,1,1)

plt.plot(data[:300])

plt.title("Original audio wave")

plt.subplot(2,1,2)

plt.plot(frequencies)

plt.title("Frequencies found")

plt.xlim(0,1200)

plt.show()

# The frequency is the number of times a wave repeats a second


frequency= 1000

noisy_freq= 50

num_samples= 48000

# The sampling rate of the analog to digital convert

sampling_rate= 48000.0

# Create the sine wave and noise

sine_wave= [np.sin(2*np.pi*frequency* x1/sampling_rate) for x1 in range(num_samples)]

sine_noise= [np.sin(2*np.pi*noisy_freq* x1/sampling_rate) for x1 in range(num_samples)]

# Convert them to numpy arrays

sine_wave= np.array(sine_wave)

sine_noise= np.array(sine_noise)

# Add them to create a noise signal


combined_signal= sine_wave+sine_noise

plt.subplot(3,1,1)

plt.title("Original sine wave")

# Need to add empty space, else eveything looks scrunched up!

plt.subplots_adjust(hspace=.5)

plt.plot(sine_wave[:500])

plt.subplot(3,1,2)

plt.title("Noisy wave")

plt.plot(sine_noise[:4000])

plt.subplot(3,1,3)

plt.title("Original + Noise")

plt.plot(combined_signal[:3000])

plt.show()
data_fft= np.fft.fft(combined_signal)

freq= (np.abs(data_fft[:len(data_fft)]))

plt.plot(freq)

plt.title("Before filtering: Will have main signal (1000Hz) + noise frequency (50Hz)")

plt.xlim(0,1200)

plt.show()

plt.close()

filtered_freq= [f if (950 < index < 1050 and f > 1)else 0 for index, f in enumerate(freq)]

plt.plot(filtered_freq)

plt.title("After filtering: Main signal only (1000Hz)")

plt.xlim(0,1200)

plt.show()
plt.close()

recovered_signal= np.fft.ifft(filtered_freq)

plt.subplot(3,1,1)

plt.title("Original sine wave")

# Need to add empty space, else everything looks scrunched up!

plt.subplots_adjust(hspace=.5)

plt.plot(sine_wave[:500])

plt.subplot(3,1,2)

plt.title("Noisy wave")

plt.plot(combined_signal[:4000])

plt.subplot(3,1,3)

plt.title("Sine wave after clean up")

plt.plot((recovered_signal[:500]))
plt.show()

You might also like