-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodage_python_2.py
81 lines (57 loc) · 2.69 KB
/
decodage_python_2.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 17 13:04:09 2018
@author: Geoffroy Leconte
"""
import librosa
import librosa.display, librosa.core, librosa.output
import matplotlib.pyplot as plt
import numpy as np
from math import *
import fonctions_utilitaires as f_uti
import os
#### chargement des données ####
# direction des fichiers audio test
audio_dir = 'C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/quelques sons/LibriSpeech/dev-clean/84/121123/'
sound_f = os.path.join(audio_dir, '84-121123-0001.flac')
import soundfile as sf
# on charge un son
y, sr = sf.read(sound_f)
l_sig = len(y)
##### Bloc pour trouver à quelle fréquence on rééchantillonne #####
freqs = librosa.core.fft_frequencies(sr=sr, n_fft=1024)
# freqs = (0, sr/n_fft, 2*sr/n_fft, …, sr/2)
cut=150
#freqs[50] = 2343.75 => on teste 5000 Hz pour l'échantillonnage
freqs1 = librosa.core.fft_frequencies(sr=5000, n_fft=1024)
#### signal s_full rééchantillonné ####
# rééchantillonnage:
sr1 = 5000
y1 = librosa.resample(y, sr, sr1)
l_sig1 = len(y1)
librosa.output.write_wav('C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/quelques sons/s_full.wav'
, y1.astype(np.float32), sr1)
#### signal basses fréquences seulement ####
D1 = librosa.stft(y1, n_fft=1024)
s_gt = librosa.istft(D1,length=l_sig1, hop_length=256)
D_low = np.copy(D1)
D_low[256:,:] = 0
s_low = librosa.istft(D_low, length=l_sig1, hop_length=256)
librosa.output.write_wav('C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/quelques sons/s_low.wav'
, s_low, sr1)
#### reconstruction SBR des hautes fréquences ####
D1_low = D1[:256,:]
# enveloppe spectrale (moyenne de chaque trame (colonne) du spectrogramme)
sp_env1 = f_uti.spectral_env(D1)
# reconstruction du signal: pour les hf, on utilise les bf, chaque trame (colonne)
# des bf est multipliée par le coefficient correspondant de l'enveloppe spectrale
# puis GL sur les hf uniquement (on connait la phase bf).
s1_recons, D1_recons = f_uti.recons_sig(D1_low, sp_env1, l_sig1, 100)
librosa.output.write_wav('C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/quelques sons/s_rec_sbr.wav'
, s1_recons, sr1)
#### affichage des SNR ####
print(' snr signal bf vs signal gt: ', f_uti.snr2(np.abs(D1), np.abs(D_low)), '\n',
'snr signal recons vs signal gt: ', f_uti.snr2(np.abs(D1), np.abs(D1_recons)))
test_out_dir = 'C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/out_sounds/out_sounds_sbr'
pipeline_recons_sig_sbr(np.abs(D1_low),np.abs(D1[256:512,:]),np.angle(D1_low),
np.angle(D1[256:512,:]), 100, test_out_dir)