Hann Window Amplitude Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mks17

//@version=5
indicator('Hann Window Amplitude Filter', overlay=false)

//Inputs

length = input(14, "Number of frequencies to Filter")
var shifts = input.int(0, "Bar moves of the Hann window")
minBase = input.float(0.1, "Filter Minimum Base", step = .1)

//Calcs

int ArrayLen = 2 * length - 1
var Array = array.new_float(ArrayLen, 0),
pi = math.pi
h = 0.0, c = 0.0, var leftside = 0, var rightside = 0, var coef = 0.0

if bar_index <= ArrayLen
    c := nz(c[1], 0) == ArrayLen - 1 ? 0 : nz(c[1], 0) + 1
    h := (1 - math.cos(2 * pi * (c - 1) / ArrayLen)) / 2 + minBase
    array.insert(Array, 0, h)

else if bar_index <= ArrayLen + 1
    if shifts > 0      
        for i = 0 to shifts - 1 by 1
            array.shift(Array)
            array.push(Array, minBase)
            shifts := shifts - 1
    else if shifts < 0
        for i = 0 to math.abs(shifts) - 1 by 1
            array.unshift(Array,minBase)
            shifts := shifts + 1

    if ArrayLen % 2 == 0
        leftside := length / 2
        rightside := ArrayLen - length / 2

    else
        leftside := math.floor(length / 2)
        rightside := ArrayLen - math.floor(length / 2)

    for i = leftside to rightside
        coef := coef + array.get(Array, i)
    for i = leftside to rightside
        array.set(Array, i, array.get(Array,i) / coef)

//Plots

plot(h)
plot(coef, color=color.purple)
plot(array.get(Array, length), title='HW', color=color.new(#FA8258, 0))
plot(bar_index % ArrayLen, title='Bar Index % Array Len', color=color.new(#FA8258, 0), display=display.data_window)
plot(c, title='Counter', color=color.new(#FA8258, 0), display=display.data_window)
plot(array.get(Array, length - 1), title='MaxCoeff +', color=color.new(#FA8258, 0), display=display.data_window)
plot(array.get(Array, length), title='MaxCoeff -', color=color.new(#FA8258, 0), display=display.data_window)
plot(array.get(Array, leftside), title='MinCoeff +', color=color.new(#FA8258, 0), display=display.data_window)
plot(array.get(Array, rightside), title='MinCoeff -', color=color.new(#FA8258, 0), display=display.data_window)
hline(0)
hline(1)
