File size: 2,188 Bytes
dfd1909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#%%
from matplotlib import pyplot as plt
import torch
from torch.nn import functional as F
import numpy as np
# %%
def smooth_lbl_loop(lbl, smooth_center, smooth_length, smooth_shape):
    lbl_new = lbl.clone().detach().float()
    lbl_copy = lbl.clone().detach()
    lbl_weight = torch.zeros(smooth_length // 2)

    for i in range(lbl_weight.size(0)):
        if smooth_shape == 'square':
            lbl_weight[i] = 1
        elif smooth_shape == 'triangle':
            lbl_weight[i] = 1 - (i + 1) / (smooth_length // 2 + 1)
        elif smooth_shape == 'hann':
            lbl_weight[i] = np.hanning(smooth_length + 2)[(smooth_length + 2) // 2 + 1 + i]

    for i in range(1, lbl_weight.size(0) + 1):
        if smooth_center:
            lbl_new[i:] += lbl_copy[:-i] * lbl_weight[i - 1]
            lbl_new[:-i] += lbl_copy[i:] * lbl_weight[i - 1]
        else:
            lbl_new[i:] += lbl_copy[:-i]

    lbl_new[lbl_new > 1] = 1

    return lbl_new

# %%
def smooth_lbl_conv(lbl, smooth_center, smooth_length, smooth_shape):
    lbl_new = lbl.clone().detach().cpu().float().unsqueeze(0)  # [N, C, L]
    lbl_weight = torch.zeros(1, 1, smooth_length)

    for i in range(lbl_weight.size(2)):
        if smooth_shape == 'square':
            lbl_weight[:, :, i] = 1
        elif smooth_shape == 'triangle':
            if i < smooth_length // 2:
                lbl_weight[:, :, i] = (i + 1) / (smooth_length // 2 + 1)
            else:
                lbl_weight[:, :, i] = 1 - (i - smooth_length // 2) / (smooth_length // 2 + 1)
        elif smooth_shape == 'hann':
            lbl_weight[:, :, i] = np.hanning(smooth_length + 2)[i + 1]

    if smooth_center:
        lbl_new = F.conv1d(lbl_new, lbl_weight, bias=None, padding=smooth_length // 2).squeeze()
    else:
        lbl_new = F.conv1d(lbl_new, lbl_weight, bias=None).squeeze()

    lbl_new[lbl_new > 1] = 1

    return lbl_new

# %%
signal = torch.randint(0, 2, (50,))
signal_smooth_loop = smooth_lbl_loop(signal, True, 3, 'triangle')
signal_smooth_conv = smooth_lbl_conv(signal, True, 3, 'triangle')

plt.plot(signal)
#plt.plot(signal_smooth_loop, 'r')
plt.plot(signal_smooth_conv.squeeze(), 'g', linestyle='--')
# %%