Single target tracking - [Tool] Hamming window

Hamming window

principle

The Hanning window can be regarded as a special case of the raised cosine window. The Hanning window can be regarded as the sum of the spectra of three rectangular time windows, or the sum of three sinc(t)-type functions, and the brackets The two terms in are moved by π/T to the left and right relative to the first spectral window, so that the side lobes cancel each other out, eliminating high-frequency interference and leakage energy.

effect

The cosine window is introduced to solve the boundary effect, and the solution is to multiply the target original pixel by a cosine window so that the pixel value near the edge is close to zero.

Code example

import math
import plotly.express as px
import numpy as np
import torch
from matplotlib import pyplot as plt


def hann1d(sz: int, centered = True) -> torch.Tensor:
    """1D cosine window."""
    if centered:
        return 0.5 * (1 - torch.cos((2 * math.pi / (sz + 1)) * torch.arange(1, sz + 1).float()))
    w = 0.5 * (1 + torch.cos((2 * math.pi / (sz + 2)) * torch.arange(0, sz//2 + 1).float()))
    return torch.cat([w, w[1:sz-sz//2].flip((0,))])


def hann2d(sz: torch.Tensor, centered = True) -> torch.Tensor:
    """2D cosine window."""
    # return hann1d(sz[0].item(), centered).reshape(1, 1, -1, 1) * hann1d(sz[1].item(), centered).reshape(1, 1, 1, -1)
    return hann1d(sz[0].item(), centered).reshape(-1, 1) * hann1d(sz[1].item(), centered).reshape(1, -1)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    h1 = hann1d(16)
    h1_numpy = h1.numpy()

    h2 = hann2d(torch.tensor([16, 16]))
    h2_numpy = h2.numpy()

    # 1D Hamming
    # plt.figure(figsize=(16, 16), dpi=80)
    # plt.show()

    # 2D Hamming
    fig = px.imshow(h2_numpy, color_continuous_scale='OrRd')
    fig.show()

Visualization

Please add image descriptionPlease add image description

Summarize

As can be seen from the above figure, the redder the pixel, the greater the weight value.

Guess you like

Origin blog.csdn.net/qq_42312574/article/details/132887348