Passing DoubleFloat types into a Julia function

Thushara_Gunaratne
Fri, 10 Feb 2023 18:59:43 +0000
New to Julia

I’m trying to create a julia function in order to optimize the implementation of a FIR filter that can handle data and coefficients represented with different Floating-Point formats (i.e. Double64, Double32, Double16, Float64, Float32, Float16, etc.). However I’m getting a Method error. I really appreciate if anyone can suggest me how to do this in a proper way.

Test_Function.jl (741 Bytes)

The Error Message I get is

ERROR: MethodError: no method matching Double64(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:266
  (::Type{T})(::ColorTypes.AbstractGray) where T<:Real at C:\Users\xxxxxxx\.julia\packages\ColorTypes\1dGw6\src\conversions.jl:115
  ...

FloatFormt = Double64; #Double32, Double16

function function_with_nongeneric_numerics(;
    FloatFormt::DataType,
    N::Int64,
    x::Vector{FloatFormt}
    )

    h = FloatFormt((0:1:N-1)/N);
    xMask = zeros(FloatFormt)(N)
    Nx = length(x);
    y = zeros(FloatFormt, Nx);
    for i in 1 : 1 : Nx
        xMask[2:end] = xMask[1:end-1];
        xMask[1] = x[i];
        y[i] = sum(xMask.*h)/N;
    end #FOR i
    return(;
    y)
end #Function

#The Body of the Main Function
Nx = 150;
N = 4;

Fs = FloatFormt(100);
Tsamp = range(start=0, step=1/Fs, length=N);

x = cospi.(2*mod.(0.125*collect(Tsamp),2))

y = function_with_nongeneric_numerics(;
    FloatFormt,
    N,
    x);

3 posts - 2 participants