Commit cdeaf664 authored by Sivalingam Thanojan's avatar Sivalingam Thanojan

Upload New File

parent 5eea171f
# -*- coding: utf-8 -*-
"""noiceReduction
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1GWWfzehgDDVuRGgTeO7sDb_l9yny3l1k
"""
!pip install numpy
!pip install pipwin
!pipwin install pyaudio
!apt-get install portaudio19-dev
!pip install sounddevice
import numpy as np
import sounddevice as sd
# parameters
duration = 5 # duration of audio recording in seconds
sample_rate = 44100 # sampling rate
block_size = 1024 # block size
alpha = 1 # spectral subtraction parameter
def spectral_subtraction(frame, noise, alpha=1):
# calculate power spectral density of the signal and noise frames
psd_frame = np.abs(np.fft.fft(frame)) ** 2
psd_noise = np.abs(np.fft.fft(noise)) ** 2
# calculate the average noise power
noise_power = np.mean(psd_noise)
# calculate the spectral subtraction gain
gain = np.maximum(1 - alpha * noise_power / psd_frame, 0)
# apply the gain to the signal frame
processed_frame = frame * gain
return processed_frame.astype(np.int16)
def process_audio(indata, outdata, frames, time, status):
# apply spectral subtraction to reduce the noise
processed_frames = spectral_subtraction(indata, noise, alpha=alpha)
# write the processed frames back to the output buffer
outdata[:] = processed_frames
# record some initial frames of noise to estimate the noise spectrum
print('Estimating noise spectrum...')
noise_frames = sd.rec(duration * sample_rate, samplerate=sample_rate, channels=1)
sd.wait() # wait for the recording to finish
noise = np.concatenate(noise_frames)
noise_spectrum = np.abs(np.fft.fft(noise)) ** 2
# start audio stream
print('Starting noise reduction...')
with sd.Stream(channels=1, blocksize=block_size, samplerate=sample_rate,
input=True, output=True, callback=process_audio):
sd.sleep(int(duration * 1000)) # wait for the recording to finish
print('Done.')
import sounddevice as sd
# print list of available input devices
print(sd.query_devices())
# set input device index (change this to the desired device)
device_idx = 0
# record audio using the specified device
audio_frames = sd.rec(duration * sample_rate, samplerate=sample_rate, channels=1, input_device=device_idx)
# get list of input devices
input_devices = sd.query_devices(kind='input')
print('Available input devices:')
for i, d in enumerate(input_devices):
print(f'{i}: {d["name"]}')
# specify the input device
device_idx = int(input('Enter device index: '))
device_info = input_devices[device_idx]
print(f'Using device {device_info["name"]} with index {device_idx}')
# record audio using the specified device
audio_frames = sd.rec(duration * sample_rate, samplerate=sample_rate, channels=1, device=device_idx)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment