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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright ©2020 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package window
import "math"
// Rectangular modifies seq in place by the Rectangular window and returns
// the result.
// See https://en.wikipedia.org/wiki/Window_function#Rectangular_window and
// https://www.recordingblogs.com/wiki/rectangular-window for details.
//
// The rectangular window has the lowest width of the main lobe and largest
// level of the side lobes. The result corresponds to a selection of
// limited length sequence of values without any modification.
//
// The sequence weights are
//
// w[k] = 1,
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 2, ΔF_0.5 = 0.89, K = 1, ɣ_max = -13, β = 0.
func Rectangular(seq []float64) []float64 {
return seq
}
// Sine modifies seq in place by the Sine window and returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Sine_window and
// https://www.recordingblogs.com/wiki/sine-window for details.
//
// Sine window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = sin(π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 3, ΔF_0.5 = 1.23, K = 1.5, ɣ_max = -23, β = -3.93.
func Sine(seq []float64) []float64 {
k := math.Pi / float64(len(seq)-1)
for i := range seq {
seq[i] *= math.Sin(k * float64(i))
}
return seq
}
// Lanczos modifies seq in place by the Lanczos window and returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Lanczos_window and
// https://www.recordingblogs.com/wiki/lanczos-window for details.
//
// The Lanczos window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = sinc(2*k/(N-1) - 1),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 3.24, ΔF_0.5 = 1.3, K = 1.62, ɣ_max = -26.4, β = -4.6.
func Lanczos(seq []float64) []float64 {
k := 2 / float64(len(seq)-1)
for i := range seq {
x := math.Pi * (k*float64(i) - 1)
if x == 0 {
// Avoid NaN.
continue
}
seq[i] *= math.Sin(x) / x
}
return seq
}
// Triangular modifies seq in place by the Triangular window and returns
// the result.
// See https://en.wikipedia.org/wiki/Window_function#Triangular_window and
// https://www.recordingblogs.com/wiki/triangular-window for details.
//
// The Triangular window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = 1 - |k/A -1|, A=(N-1)/2,
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.33, K = 2, ɣ_max = -26.5, β = -6.
func Triangular(seq []float64) []float64 {
a := float64(len(seq)-1) / 2
for i := range seq {
seq[i] *= 1 - math.Abs(float64(i)/a-1)
}
return seq
}
// Hann modifies seq in place by the Hann window and returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
// and https://www.recordingblogs.com/wiki/hann-window for details.
//
// The Hann window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = 0.5*(1 - cos(2*π*k/(N-1))),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.5, K = 2, ɣ_max = -31.5, β = -6.
func Hann(seq []float64) []float64 {
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
seq[i] *= 0.5 * (1 - math.Cos(k*float64(i)))
}
return seq
}
// BartlettHann modifies seq in place by the Bartlett-Hann window and returns
// result.
// See https://en.wikipedia.org/wiki/Window_function#Bartlett%E2%80%93Hann_window
// and https://www.recordingblogs.com/wiki/bartlett-hann-window for details.
//
// The Bartlett-Hann window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = 0.62 - 0.48*|k/(N-1)-0.5| - 0.38*cos(2*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.45, K = 2, ɣ_max = -35.9, β = -6.
func BartlettHann(seq []float64) []float64 {
const (
a0 = 0.62
a1 = 0.48
a2 = 0.38
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
seq[i] *= a0 - a1*math.Abs(float64(i)/float64(len(seq)-1)-0.5) - a2*math.Cos(k*float64(i))
}
return seq
}
// Hamming modifies seq in place by the Hamming window and returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
// and https://www.recordingblogs.com/wiki/hamming-window for details.
//
// The Hamming window is a high-resolution window. Among K=2 windows it has
// the highest ɣ_max.
//
// The sequence weights are
//
// w[k] = 25/46 - 21/46 * cos(2*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.33, K = 2, ɣ_max = -42, β = -5.37.
func Hamming(seq []float64) []float64 {
const (
a0 = 0.54
a1 = 0.46
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
seq[i] *= a0 - a1*math.Cos(k*float64(i))
}
return seq
}
// Blackman modifies seq in place by the Blackman window and returns the
// result.
// See https://en.wikipedia.org/wiki/Window_function#Blackman_window and
// https://www.recordingblogs.com/wiki/blackman-window for details.
//
// The Blackman window is a high-resolution window.
//
// The sequence weights are
//
// w[k] = 0.42 - 0.5*cos(2*π*k/(N-1)) + 0.08*cos(4*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 6, ΔF_0.5 = 1.7, K = 3, ɣ_max = -58, β = -7.54.
func Blackman(seq []float64) []float64 {
const (
a0 = 0.42
a1 = 0.5
a2 = 0.08
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
x := k * float64(i)
seq[i] *= a0 - a1*math.Cos(x) + a2*math.Cos(2*x)
}
return seq
}
// BlackmanHarris modifies seq in place by the Blackman-Harris window and
// returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window
// and https://www.recordingblogs.com/wiki/blackman-harris-window for details.
//
// The Blackman-Harris window is a low-resolution window.
//
// The sequence weights are
//
// w[k] = 0.35875 - 0.48829*cos(2*π*k/(N-1)) +
// 0.14128*cos(4*π*k/(N-1)) - 0.01168*cos(6*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 8, ΔF_0.5 = 1.97, K = 4, ɣ_max = -92, β = -8.91.
func BlackmanHarris(seq []float64) []float64 {
const (
a0 = 0.35875
a1 = 0.48829
a2 = 0.14128
a3 = 0.01168
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
x := k * float64(i)
seq[i] *= a0 - a1*math.Cos(x) + a2*math.Cos(2*x) - a3*math.Cos(3*x)
}
return seq
}
// Nuttall modifies seq in place by the Nuttall window and returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Nuttall_window,_continuous_first_derivative
// and https://www.recordingblogs.com/wiki/nuttall-window for details.
//
// The Nuttall window is a low-resolution window.
//
// The sequence weights are
//
// w[k] = 0.355768 - 0.487396*cos(2*π*k/(N-1)) + 0.144232*cos(4*π*k/(N-1)) -
// 0.012604*cos(6*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 8, ΔF_0.5 = 1.98, K = 4, ɣ_max = -93, β = -9.
func Nuttall(seq []float64) []float64 {
const (
a0 = 0.355768
a1 = 0.487396
a2 = 0.144232
a3 = 0.012604
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
x := k * float64(i)
seq[i] *= a0 - a1*math.Cos(x) + a2*math.Cos(2*x) - a3*math.Cos(3*x)
}
return seq
}
// BlackmanNuttall modifies seq in place by the Blackman-Nuttall window and
// returns the result.
// See https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Nuttall_window
// and https://www.recordingblogs.com/wiki/blackman-nuttall-window for details.
//
// The Blackman-Nuttall window is a low-resolution window.
//
// The sequence weights are
//
// w[k] = 0.3635819 - 0.4891775*cos(2*π*k/(N-1)) + 0.1365995*cos(4*π*k/(N-1)) -
// 0.0106411*cos(6*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 8, ΔF_0.5 = 1.94, K = 4, ɣ_max = -98, β = -8.8.
func BlackmanNuttall(seq []float64) []float64 {
const (
a0 = 0.3635819
a1 = 0.4891775
a2 = 0.1365995
a3 = 0.0106411
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
x := k * float64(i)
seq[i] *= a0 - a1*math.Cos(x) + a2*math.Cos(2*x) - a3*math.Cos(3*x)
}
return seq
}
// FlatTop modifies seq in place by the Flat Top window and returns the
// result.
// See https://en.wikipedia.org/wiki/Window_function#Flat_top_window and
// https://www.recordingblogs.com/wiki/flat-top-window for details.
//
// The Flat Top window is a low-resolution window.
//
// The sequence weights are
//
// w[k] = 0.21557895 - 0.41663158*cos(2*π*k/(N-1)) +
// 0.277263158*cos(4*π*k/(N-1)) - 0.083578947*cos(6*π*k/(N-1)) +
// 0.006947368*cos(4*π*k/(N-1)),
//
// for k=0,1,...,N-1 where N is the length of the window.
//
// Spectral leakage parameters: ΔF_0 = 10, ΔF_0.5 = 3.72, K = 5, ɣ_max = -93.0, β = -13.34.
func FlatTop(seq []float64) []float64 {
const (
a0 = 0.21557895
a1 = 0.41663158
a2 = 0.277263158
a3 = 0.083578947
a4 = 0.006947368
)
k := 2 * math.Pi / float64(len(seq)-1)
for i := range seq {
x := k * float64(i)
seq[i] *= a0 - a1*math.Cos(x) + a2*math.Cos(2*x) - a3*math.Cos(3*x) + a4*math.Cos(4*x)
}
return seq
}
|