-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioSignal.py
82 lines (59 loc) · 2 KB
/
AudioSignal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from scipy.fftpack import *
import numpy as np
class AudioSignal:
__count = 0
def __init__(self, filename, sample_rate, amplitudes):
self.__filename = filename
self.__sample_rate = sample_rate
if len(amplitudes.shape) == 2:
amplitudes = (amplitudes.sum(axis=1) / 2)
self.__amplitudes = amplitudes
AudioSignal.__count += 1
def get_filename(self):
return self.__filename
def get_sample_rate(self):
return self.__sample_rate
def set_sample_rate(self, sample_rate):
self.__sample_rate = sample_rate
def get_amplitudes(self):
return self.__amplitudes
def set_amplitudes(self, amplitudes):
self.__amplitudes = amplitudes
def get_fourier_transform(self):
return fftshift(fft(self.get_amplitudes()))
def get_energy(self):
return np.abs(self.get_fourier_transform()) ** 2
@staticmethod
def display_count():
return AudioSignal.__count
def __len__(self):
return len(self.get_amplitudes())
def lpf(signal, bandwidth):
"""
Return a base-band signal
Parameters
----------
:type signal: np.core.ndarray
:type bandwidth: float
"""
length = len(signal)
low_pass = np.linspace(-length // 2, length // 2, num=length)
low_pass = np.where(abs(low_pass) <= bandwidth, 1, 0)
return signal * low_pass
def bpf(signal, frequency, bandwidth):
"""
Return a bandpass signal
Parameters
----------
:type signal: np.core.ndarray
:type frequency: float
:type bandwidth: float
"""
if len(signal.shape) == 2:
signal = signal.sum(axis=1) / 2
length = len(signal)
band_pass = np.linspace(-length // 2, length // 2, num=length)
band_pass = np.where(abs(band_pass) <= frequency + bandwidth, 1, 0) - np.where(
abs(band_pass) < frequency - bandwidth, 1, 0)
print(band_pass)
return signal * band_pass