RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
rapp_energy_denoise.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Manos Tsardoulias
19 # contact: etsardou@iti.gr
20 
21 import sys
22 import time
23 import os
24 from pylab import *
25 from scipy.io import wavfile
26 
27 ## @class EnergyDenoise
28 # Performs energy denoising on an audio file
30 
31  ## Performs energy-based denoising
32  #
33  # Handles service callback
34  # rapp_audio_processing.AudioProcessing#energyDenoiseCallback
35  #
36  # @param audio_file [string] Audio file path
37  # @param scale [float] Energy denoise scale
38  # @param denoised_audio_file [string] Path to write denoised audio file
39  def energyDenoise(self, audio_file, scale, denoised_audio_file, energy_denoising_debug):
40  if not os.path.isfile(audio_file):
41  return False
42  samp_freq, signal = wavfile.read(audio_file)
43  samples = signal.shape[0]
44  sq_signal = signal * 1.0
45 
46  if energy_denoising_debug:
47  timearray = arange(0, samples*1.0, 1)
48  timearray /= samp_freq
49  timearray *= 1000.0
50  subplot(3,1,1)
51  plot(timearray, signal, color = 'k')
52 
53  for i in range(0, len(sq_signal)):
54  sq_signal[i] *= sq_signal[i]
55  mean_sq = mean(sq_signal)
56 
57  for i in range(0, len(sq_signal)):
58  if sq_signal[i] < scale * mean_sq:
59  signal[i] = 0
60 
61  if energy_denoising_debug:
62  timearray = arange(0, samples*1.0, 1)
63  timearray /= samp_freq
64  timearray *= 1000.0
65  subplot(3,1,2)
66  plot(timearray, signal, color = 'k')
67 
68  if energy_denoising_debug:
69  show()
70 
71  wavfile.write(denoised_audio_file, samp_freq, signal)
72 
73  return True
74 
Performs energy denoising on an audio file.