File tree Expand file tree Collapse file tree 10 files changed +209
-0
lines changed Expand file tree Collapse file tree 10 files changed +209
-0
lines changed Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+
4
+ #1-1
5
+ a = 1.0
6
+ freq = 440
7
+ samp_rate = 16000
8
+ d = 3
9
+
10
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
11
+
12
+
13
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
14
+
15
+
16
+ plt .plot (t ,sin_wave )
17
+ plt .title ("Sin Wave" )
18
+ plt .xlabel ("Time [s]" )
19
+ plt .ylabel ("Amplitude" )
20
+ plt .show ()
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ #1-2
4
+ import wave
5
+
6
+ a = 1.0
7
+ freq = 440
8
+ samp_rate = 16000
9
+ d = 3
10
+
11
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
12
+
13
+
14
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
15
+ output = 'sin_wave_440Hz.wav'
16
+ with wave .open (output , 'w' ) as wf :
17
+ wf .setnchannels (1 )
18
+ wf .setsampwidth (2 )
19
+ wf .setframerate (samp_rate )
20
+ wf .writeframes ((sin_wave * 32767 ).astype (np .int16 ).tobytes ())
21
+
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+ a = 1.0
5
+ freq = 440
6
+ samp_rate = 16000
7
+ d = 3
8
+
9
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
10
+
11
+
12
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
13
+ #1-3
14
+ freq2 = 660
15
+ sin_wave2 = a * np .sin (2 * np .pi * freq2 * t )
16
+
17
+
18
+ stereo_wave = np .vstack ((sin_wave , sin_wave2 )).T
19
+
20
+
21
+ output_stereo = 'stereo_sin_waves.wav'
22
+ with wave .open (output_stereo , 'w' ) as wf :
23
+ wf .setnchannels (2 )
24
+ wf .setsampwidth (2 )
25
+ wf .setframerate (samp_rate )
26
+ wf .writeframes ((stereo_wave * 32767 ).astype (np .int16 ).tobytes ())
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+
5
+ a = 1.0
6
+ freq = 440
7
+ samp_rate = 16000
8
+ d = 3
9
+
10
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
11
+
12
+
13
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
14
+
15
+ #1-4
16
+ white_noise = np .random .normal (0 , 1 , len (t ))
17
+
18
+
19
+ plt .plot (t , white_noise )
20
+ plt .title ("White Noise" )
21
+ plt .xlabel ("Time [s]" )
22
+ plt .ylabel ("Amplitude" )
23
+ plt .show ()
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+ a = 1.0
5
+ freq = 440
6
+ samp_rate = 16000
7
+ d = 3
8
+
9
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
10
+
11
+
12
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
13
+
14
+ white_noise = np .random .normal (0 , 1 , len (t ))
15
+ #1-5
16
+ mixed_signal = sin_wave + white_noise
17
+
18
+
19
+ plt .plot (t , mixed_signal )
20
+ plt .title ("Mixed Signal (Sin Wave + White Noise)" )
21
+ plt .xlabel ("Time [s]" )
22
+ plt .ylabel ("Amplitude" )
23
+ plt .show ()
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+
5
+ def calculate_snr (signal , noise ):
6
+ s_power = np .sum (signal ** 2 ) / len (signal )
7
+ n_power = np .sum (noise ** 2 ) / len (noise )
8
+ snr = 10 * np .log10 (s_power / n_power )
9
+ return snr
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+
5
+ #1-7
6
+ def add_noise_with_snr (signal , desired_snr_db ):
7
+ s_power = np .sum (signal ** 2 ) / len (signal )
8
+ snr_linear = 10 ** (desired_snr_db / 10 )
9
+ n_power = s_power / snr_linear
10
+ noise = np .random .normal (0 , np .sqrt (n_power ), len (signal ))
11
+ return signal + noise
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+
5
+
6
+ a = 1.0
7
+ freq = 440
8
+ samp_rate = 16000
9
+ d = 3
10
+
11
+ t = np .linspace (0 , d , int (samp_rate * d ), endpoint = False )
12
+
13
+
14
+ sin_wave = a * np .sin (2 * np .pi * freq * t )
15
+
16
+
17
+ def add_noise_with_snr (signal , desired_snr_db ):
18
+ s_power = np .sum (signal ** 2 ) / len (signal )
19
+ snr_linear = 10 ** (desired_snr_db / 10 )
20
+ n_power = s_power / snr_linear
21
+ noise = np .random .normal (0 , np .sqrt (n_power ), len (signal ))
22
+ return signal + noise
23
+
24
+ #1-8
25
+ desired_snr = 6
26
+ noise_signal = add_noise_with_snr (sin_wave , desired_snr )
27
+
28
+ output_noise = 'sin_wave_with_noise_6dB.wav'
29
+ with wave .open (output_noise , 'w' ) as wf :
30
+ wf .setnchannels (1 )
31
+ wf .setsampwidth (2 )
32
+ wf .setframerate (samp_rate )
33
+ wf .writeframes ((noise_signal * 32767 ).astype (np .int16 ).tobytes ())
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+
5
+ #1-9
6
+ from scipy .io import wavfile
7
+ output_noise = 'sin_wave_with_noise_6dB.wav'
8
+ rate , data = wavfile .read (output_noise )
9
+
10
+
11
+ downsampled_data = data [::2 ]
12
+
13
+
14
+ output_downsampled = 'downsampled_8kHz.wav'
15
+ wavfile .write (output_downsampled , 8000 , downsampled_data .astype (np .int16 ))
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ import wave
4
+ from scipy .io import wavfile
5
+
6
+ output_noise = 'sin_wave_with_noise_6dB.wav'
7
+ rate , data = wavfile .read (output_noise )
8
+
9
+
10
+ downsampled_data = data [::2 ]
11
+ #1-10
12
+ filtered_signal = np .convolve (downsampled_data , np .ones (5 )/ 5 , mode = 'valid' )
13
+
14
+ plt .figure (figsize = (14 , 6 ))
15
+ plt .subplot (2 , 1 , 1 )
16
+ plt .plot (downsampled_data [:1000 ])
17
+ plt .title ("Original Downsampled Signal (8 kHz)" )
18
+ plt .xlabel ("Sample" )
19
+ plt .ylabel ("Amplitude" )
20
+
21
+ plt .subplot (2 , 1 , 2 )
22
+ plt .plot (filtered_signal [:1000 ])
23
+ plt .title ("Filtered Signal (5-point Moving Average)" )
24
+ plt .xlabel ("Sample" )
25
+ plt .ylabel ("Amplitude" )
26
+
27
+ plt .tight_layout ()
28
+ plt .show ()
You can’t perform that action at this time.
0 commit comments