HammingWindow¶
HammingWindow - 29¶
Version¶
name: HammingWindow (GitHub)
domain:
mainsince_version:
29function:
Truesupport_level:
SupportType.COMMONshape inference:
True
This version of the operator has been available since version 29.
Summary¶
Generates a one-dimensional Hamming window of length N, where N is the
positive integer specified by the scalar input size.
For each integer index n with 0 <= n < N, the window value is
w[n] = alpha - beta * cos(2 * pi * n / D)
where D = N when periodic = 1, and D = N - 1 when periodic = 0.
A periodic window is equivalent to the first N elements of a symmetric window
of length N + 1.
The window values are cast to output_datatype. The output tensor has shape [N].
The default coefficients alpha = 0.54 and beta = 0.46 follow equation (30b),
Section V-D of Harris (1978).
Setting alpha = 25/46 and beta = 21/46 gives the coefficients used by
HammingWindow-17.
Function Body¶
The function definition for this operator.
<
domain: "",
opset_import: ["" : 29]
>
HammingWindow <beta,alpha,periodic,output_datatype>(size) => (output)
{
A0 = Constant <value_float: float = @alpha> ()
A1 = Constant <value_float: float = @beta> ()
Zero = Constant <value: tensor = float {0}> ()
One = Constant <value: tensor = float {1}> ()
Tau = Constant <value: tensor = float {6.2831855}> ()
Periodic_Size_FP = Cast <to: int = 1> (size)
Symmetric_Size_FP = Sub (Periodic_Size_FP, One)
IsPeriodic = Constant <value_int: int = @periodic> ()
IsPeriodic_FP = Cast <to: int = 1> (IsPeriodic)
IsSymmetric_FP = Sub (One, IsPeriodic_FP)
Periodic_Component = Mul (Periodic_Size_FP, IsPeriodic_FP)
Symmetric_Component = Mul (Symmetric_Size_FP, IsSymmetric_FP)
Size_FP = Add (Periodic_Component, Symmetric_Component)
AngularIncrement = Div (Tau, Size_FP)
Range = Range (Zero, Periodic_Size_FP, One)
RangeAngular = Mul (Range, AngularIncrement)
CosRangeAngular = Cos (RangeAngular)
A1_Component = Mul (A1, CosRangeAngular)
Window = Sub (A0, A1_Component)
output = Cast <to: int = @output_datatype> (Window)
}
Attributes¶
alpha - FLOAT (default is
0.54):The constant coefficient
alphaof the window. The default value is 0.54.beta - FLOAT (default is
0.46):The cosine coefficient
betaof the window. The default value is 0.46.output_datatype - INT (default is
1):The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T2. The default value is 1 = FLOAT.
periodic - INT (default is
1):If 1, returns a periodic window of length size, equivalent to the first size elements of a symmetric window of length size + 1. If 0, returns a symmetric window of length size. The default value is 1.
Inputs¶
size (heterogeneous) - T1:
A scalar value indicating the length of the window.
Outputs¶
output (heterogeneous) - T2:
A Hamming window with length: size. The output has the shape: [size].
Type Constraints¶
T1 in (
tensor(int32),tensor(int64)):Constrain the input size to int32_t or int64_t.
T2 in (
tensor(bfloat16),tensor(double),tensor(float),tensor(float16),tensor(int16),tensor(int32),tensor(int64),tensor(int8),tensor(uint16),tensor(uint32),tensor(uint64),tensor(uint8)):Constrain output types to numeric tensors.
Examples¶
default¶
import numpy as np
import onnx
# Test periodic window
node = onnx.helper.make_node(
"HammingWindow",
inputs=["x"],
outputs=["y"],
)
size = np.int32(10)
a0 = 0.54
a1 = 0.46
y = a0 - a1 * np.cos(2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size)
expect(
node,
inputs=[size],
outputs=[y.astype(np.float32)],
name="test_hammingwindow",
)
# Test symmetric window
node = onnx.helper.make_node(
"HammingWindow", inputs=["x"], outputs=["y"], periodic=0
)
size = np.int32(10)
a0 = 0.54
a1 = 0.46
y = a0 - a1 * np.cos(
2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / (size - 1)
)
expect(
node,
inputs=[size],
outputs=[y.astype(np.float32)],
name="test_hammingwindow_symmetric",
)
_alpha_beta¶
import numpy as np
import onnx
# Test custom coefficients
node = onnx.helper.make_node(
"HammingWindow", inputs=["x"], outputs=["y"], alpha=0.5, beta=0.5
)
size = np.int32(10)
y = 0.5 - 0.5 * np.cos(
2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size
)
expect(
node,
inputs=[size],
outputs=[y.astype(np.float32)],
name="test_hammingwindow_alpha_beta",
)
_v17¶
import numpy as np
import onnx
# Keep the legacy coefficients covered independently of the latest schema.
for periodic in (0, 1):
node = onnx.helper.make_node(
"HammingWindow", ["x"], ["y"], periodic=periodic
)
size = np.int32(10)
denominator = size if periodic else size - 1
y = 25 / 46 - (21 / 46) * np.cos(
2 * np.pi * np.arange(size, dtype=np.float32) / denominator
)
expect(
node,
inputs=[size],
outputs=[y.astype(np.float32)],
name=f"test_hammingwindow_v17_periodic_{periodic}",
opset_imports=[onnx.helper.make_opsetid("", 17)],
)
HammingWindow - 17¶
Version¶
name: HammingWindow (GitHub)
domain:
mainsince_version:
17function:
Truesupport_level:
SupportType.COMMONshape inference:
True
This version of the operator has been available since version 17.
Summary¶
Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106.
Function Body¶
The function definition for this operator.
<
domain: "",
opset_import: ["" : 17]
>
HammingWindow <periodic,output_datatype>(size) => (output)
{
A0 = Constant <value: tensor = float {0.54347825}> ()
A1 = Constant <value: tensor = float {0.45652175}> ()
A2 = Constant <value: tensor = float {0}> ()
Zero = Constant <value: tensor = float {0}> ()
One = Constant <value: tensor = float {1}> ()
Two = Constant <value: tensor = float {2}> ()
Tau = Constant <value: tensor = float {6.2831855}> ()
Periodic_Size_FP = Cast <to: int = 1> (size)
Symmetric_Size_FP = Sub (Periodic_Size_FP, One)
IsPeriodic = Constant <value_int: int = @periodic> ()
IsPeriodic_FP = Cast <to: int = 1> (IsPeriodic)
IsSymmetric_FP = Sub (One, IsPeriodic_FP)
Periodic_Component = Mul (Periodic_Size_FP, IsPeriodic_FP)
Symmetric_Component = Mul (Symmetric_Size_FP, IsSymmetric_FP)
Size_FP = Add (Periodic_Component, Symmetric_Component)
AngularIncrement = Div (Tau, Size_FP)
Range = Range (Zero, Periodic_Size_FP, One)
RangeAngular = Mul (Range, AngularIncrement)
TwoRangeAngular = Mul (RangeAngular, Two)
CosTwoRangeAngular = Cos (TwoRangeAngular)
A2_Component = Mul (A2, CosTwoRangeAngular)
CosRangeAngular = Cos (RangeAngular)
A1_Component = Mul (A1, CosRangeAngular)
Temp0 = Sub (A0, A1_Component)
Temp1 = Add (Temp0, A2_Component)
output = Cast <to: int = @output_datatype> (Temp1)
}
Attributes¶
output_datatype - INT (default is
1):The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T2. The default value is 1 = FLOAT.
periodic - INT (default is
1):If 1, returns a periodic window of length size, equivalent to the first size elements of a symmetric window of length size + 1. If 0, returns a symmetric window of length size. The default value is 1.
Inputs¶
size (heterogeneous) - T1:
A scalar value indicating the length of the window.
Outputs¶
output (heterogeneous) - T2:
A Hamming window with length: size. The output has the shape: [size].
Type Constraints¶
T1 in (
tensor(int32),tensor(int64)):Constrain the input size to int32_t or int64_t.
T2 in (
tensor(bfloat16),tensor(double),tensor(float),tensor(float16),tensor(int16),tensor(int32),tensor(int64),tensor(int8),tensor(uint16),tensor(uint32),tensor(uint64),tensor(uint8)):Constrain output types to numeric tensors.