PRIYANSHUDHAKED commited on
Commit
639713f
·
verified ·
1 Parent(s): d32cc6d

Delete src/miocodec/pupu/modules/anti_aliasing/filter.py with huggingface_hub

Browse files
src/miocodec/pupu/modules/anti_aliasing/filter.py DELETED
@@ -1,94 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
- import math
5
-
6
- if "sinc" in dir(torch):
7
- sinc = torch.sinc
8
- else:
9
- # This code is adopted from adefossez's julius.core.sinc under the MIT License
10
- # https://adefossez.github.io/julius/julius/core.html
11
- def sinc(x: torch.Tensor):
12
- """
13
- Implementation of sinc, i.e. sin(pi * x) / (pi * x)
14
- __Warning__: Different to julius.sinc, the input is multiplied by `pi`!
15
- """
16
- return torch.where(
17
- x == 0,
18
- torch.tensor(1.0, device=x.device, dtype=x.dtype),
19
- torch.sin(math.pi * x) / math.pi / x,
20
- )
21
-
22
-
23
- # This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License
24
- # https://adefossez.github.io/julius/julius/lowpass.html
25
- def kaiser_sinc_filter1d(
26
- cutoff, half_width, kernel_size
27
- ): # return filter [1,1,kernel_size]
28
- even = kernel_size % 2 == 0
29
- half_size = kernel_size // 2
30
-
31
- # For kaiser window
32
- delta_f = 4 * half_width
33
- A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95
34
- if A > 50.0:
35
- beta = 0.1102 * (A - 8.7)
36
- elif A >= 21.0:
37
- beta = 0.5842 * (A - 21) ** 0.4 + 0.07886 * (A - 21.0)
38
- else:
39
- beta = 0.0
40
- window = torch.kaiser_window(kernel_size, beta=beta, periodic=False)
41
-
42
- # ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio
43
- if even:
44
- time = torch.arange(-half_size, half_size) + 0.5
45
- else:
46
- time = torch.arange(kernel_size) - half_size
47
- if cutoff == 0:
48
- filter_ = torch.zeros_like(time)
49
- else:
50
- filter_ = 2 * cutoff * window * sinc(2 * cutoff * time)
51
- # Normalize filter to have sum = 1, otherwise we will have a small leakage
52
- # of the constant component in the input signal.
53
- filter_ /= filter_.sum()
54
- filter = filter_.view(1, 1, kernel_size)
55
-
56
- return filter
57
-
58
-
59
- class LowPassFilter1d(nn.Module):
60
- def __init__(
61
- self,
62
- cutoff=0.5,
63
- half_width=0.6,
64
- stride: int = 1,
65
- padding: bool = True,
66
- padding_mode: str = "replicate",
67
- kernel_size: int = 12,
68
- ):
69
- # kernel_size should be even number for stylegan3 setup,
70
- # in this implementation, odd number is also possible.
71
- super().__init__()
72
- if cutoff < -0.0:
73
- raise ValueError("Minimum cutoff must be larger than zero.")
74
- if cutoff > 0.5:
75
- raise ValueError("A cutoff above 0.5 does not make sense.")
76
- self.kernel_size = kernel_size
77
- self.even = kernel_size % 2 == 0
78
- self.pad_left = kernel_size // 2 - int(self.even)
79
- self.pad_right = kernel_size // 2
80
- self.stride = stride
81
- self.padding = padding
82
- self.padding_mode = padding_mode
83
- filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size)
84
- self.register_buffer("filter", filter)
85
-
86
- # input [B, C, T]
87
- def forward(self, x):
88
- _, C, _ = x.shape
89
-
90
- if self.padding:
91
- x = F.pad(x, (self.pad_left, self.pad_right), mode=self.padding_mode)
92
- out = F.conv1d(x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
93
-
94
- return out