Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
Smart E- Learn Tracer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
23_22 - J 01
Smart E- Learn Tracer
Commits
5eea171f
Commit
5eea171f
authored
May 15, 2023
by
Sivalingam Thanojan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
552f1524
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
+74
-0
noise.py
noise.py
+74
-0
No files found.
noise.py
0 → 100644
View file @
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment