a little error in the documentation

40 views
Skip to first unread message

Jason Kuan

unread,
Dec 7, 2023, 11:53:22 AM12/7/23
to gonum-dev
Hi

I referred to the sample code provided at https://pkg.go.dev/gonum.org/v1/gonum/fourier to implement FFT2D, and I found that the code example in the documentation is incorrect. The example uses an 11x11 image as input, but there's an error when the number of rows and columns are different. I modified the code as follows. Is there someone specifically responsible for maintaining this, or can I submit a PR to a particular repo?

```
func main() {
// Image is a set of diagonal lines.
image := mat.NewDense(11, 11, []float64{
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
})

// Make appropriately sized complex FFT.
// Rows and columns are the same, so the same
// CmplxFFT can be used for both axes.
r, c := image.Dims()
cfftCol := fourier.NewCmplxFFT(c)
    cfftRow := fourier.NewCmplxFFT(r)

// Perform the first axis transform.
rows := make([]complex128, r*c)
for i := 0; i < r; i++ {
row := rows[c*i : c*(i+1)]
for j, v := range image.RawRowView(i) {
row[j] = complex(v, 0)
}
cfftCol.Coefficients(row, row)
}

// Perform the second axis transform, storing
// the result in freqs.
freqs := mat.NewDense(r, c, nil)
column := make([]complex128, r)
for j := 0; j < c; j++ {
for i := 0; i < r; i++ {
column[i] = rows[i*c+j]
}
cfftRow.Coefficients(column, column)
for i, v := range column {
// Center the frequencies.
freqs.Set(cfftRow.UnshiftIdx(i), cfftCol.UnshiftIdx(j), scalar.Round(cmplx.Abs(v), 1))
}
}

fmt.Printf("%v\n", mat.Formatted(freqs))

}

```

Dan Kortschak

unread,
Dec 7, 2023, 1:41:49 PM12/7/23
to gonu...@googlegroups.com
On Thu, 2023-12-07 at 07:25 -0800, Jason Kuan wrote:
> Hi
>
> I referred to the sample code provided at
> https://pkg.go.dev/gonum.org/v1/gonum/fourier to implement FFT2D, and
> I found that the code example in the documentation is incorrect. The
> example uses an 11x11 image as input, but there's an error when the
> number of rows and columns are different. I modified the code as
> follows. Is there someone specifically responsible for maintaining
> this, or can I submit a PR to a particular repo?

Issues can be filed at the project's GitHub site,
https://github.com/gonum/gonum/issues. What do you believe the error is
here?

Dan


Reply all
Reply to author
Forward
0 new messages