This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
snr.py
66 lines (52 loc) · 2.16 KB
/
snr.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
# Copyright (C) 2021-22 Intel Corporation
# SPDX-License-Identifier: MIT
# See: https://spdx.org/licenses/
import numpy as np
import torch
from typing import Union
EPS = 1e-8
def si_snr(target: Union[torch.tensor, np.ndarray],
estimate: Union[torch.tensor, np.ndarray]) -> torch.tensor:
"""Calculates SI-SNR estiamte from target audio and estiamte audio. The
audio sequene is expected to be a tensor/array of dimension more than 1.
The last dimension is interpreted as time.
The implementation is based on the example here:
https://www.tutorialexample.com/wp-content/uploads/2021/12/SI-SNR-definition.png
Parameters
----------
target : Union[torch.tensor, np.ndarray]
Target audio waveform.
estimate : Union[torch.tensor, np.ndarray]
Estimate audio waveform.
Returns
-------
torch.tensor
SI-SNR of each target and estimate pair.
"""
if not torch.is_tensor(target):
target: torch.tensor = torch.tensor(target)
if not torch.is_tensor(estimate):
estimate: torch.tensor = torch.tensor(estimate)
# zero mean to ensure scale invariance
s_target = target - torch.mean(target, dim=-1, keepdim=True)
s_estimate = estimate - torch.mean(estimate, dim=-1, keepdim=True)
# <s, s'> / ||s||**2 * s
pair_wise_dot = torch.sum(s_target * s_estimate, dim=-1, keepdim=True)
s_target_norm = torch.sum(s_target ** 2, dim=-1, keepdim=True)
pair_wise_proj = pair_wise_dot * s_target / s_target_norm
e_noise = s_estimate - pair_wise_proj
pair_wise_sdr = torch.sum(pair_wise_proj ** 2,
dim=-1) / (torch.sum(e_noise ** 2,
dim=-1) + EPS)
return 10 * torch.log10(pair_wise_sdr + EPS)
def sdr(estimate, target):
# x: clean speech target
# n: noise signal
# y = x + n: noisy input
# \hat{x} : estimate signal
# x_target = <x, \hat{x}> / ||x||^2 * x
# e_noise = <n, \hat{x}> / ||n||^2 * n
# e_artif = \hat{x} - x_target - e_noise
# sdr = 10 log10(||x_target||^2 / ||e_noise + e_artif||^2)
# = 10 log10(||x_target||^2 / ||\hat{x} - x_target||^2)
pass