3030# See also: IDST
3131
3232module DST
33- using AbstractFFTs, FFTW
33+ function _ensure_fftw()
34+ if ! isdefined(@__MODULE__, :FFTW)
35+ Core. eval(Main, :(using FFTW))
36+ Core. eval(@__MODULE__, :(const FFTW = Main. FFTW))
37+ end
38+ return nothing
39+ end
3440
35- export dst, dst_old
41+ export dst, dst!, dst_u, plan_dst, dst_old
42+
43+ """
44+ dst(x; dims=1)
45+
46+ Compute the Type-I Discrete Sine Transform (DST) of `x` along `dims`.
3647
48+ Arguments
49+ - `x`: an `AbstractArray` (vector or matrix).
50+ - `dims`: dimension to transform (default `1`).
51+
52+ Returns
53+ - transformed array of the same shape as `x`.
54+
55+ Example
56+ ```julia
57+ y = dst([1.0,2.0,3.0])
58+ ```
59+ """
3760 function dst(x:: AbstractArray , dims= 1 )
61+ _ensure_fftw()
3862 N = size(x, dims)
3963 return FFTW. r2r(x, FFTW. RODFT00, dims) / 2
4064 end
4165
4266 function dst_u(x:: AbstractArray , dims= 1 )
67+ _ensure_fftw()
4368 N = size(x, dims)
4469 new_x = FFTW. r2r(x, FFTW. RODFT00, dims)
4570
4671 return new_x / (sqrt(2 * N)) # unitory value
4772 end
4873
74+ """
75+ plan_dst(x; dims=1, flags=FFTW.MEASURE)
76+
77+ Create an FFTW r2r plan for the DST (RODFT00) on `x`. The returned plan
78+ can be executed with `plan * x`.
79+ """
80+ function plan_dst(x:: AbstractArray ; dims= 1 , flags= FFTW. MEASURE)
81+ _ensure_fftw()
82+ return FFTW. r2r_plan(x, FFTW. RODFT00, dims; flags= flags)
83+ end
84+
85+ """
86+ dst!(x; dims=1)
87+
88+ In-place DST that overwrites `x` with its transform when possible. Falls
89+ back to a safe out-of-place compute and copy if the in-place routine is
90+ not available on the current platform.
91+ """
92+ function dst!(x:: AbstractArray ; dims= 1 )
93+ _ensure_fftw()
94+ try
95+ # Try to use FFTW in-place r2r if available
96+ FFTW. r2r!(x, FFTW. RODFT00, dims)
97+ x .*= 0.5
98+ return x
99+ catch _
100+ # Fallback: compute out-of-place and copy into x
101+ tmp = FFTW. r2r(x, FFTW. RODFT00, dims)
102+ tmp .*= 0.5
103+ x .= tmp
104+ return x
105+ end
106+ end
107+
49108 function dst_old(a:: AbstractArray , n= nothing )
109+ _ensure_fftw()
50110 if minimum(size(a)) == 1
51111 if size(a, 2 ) > 1
52112 do_trans = 1
@@ -73,7 +133,7 @@ module DST
73133 y = zeros(2 * (n + 1 ), m)
74134 y[2 : n+ 1 , :] = aa
75135 y[n+ 3 : 2 * (n+ 1 ), :] = - reverse(aa, dims= 1 )
76- yy = fft(y, (1 ,))
136+ yy = FFTW . fft(y, (1 ,))
77137 b = yy[2 : n+ 1 , :] / (- 2 * sqrt(- 1 + 0im ))
78138
79139
0 commit comments