Skip to content

Commit 77d929f

Browse files
committed
Fix bug in FFT
The FFT routine does not work for odd N Solution is to add DFT and use it when N is odd
1 parent 6d654d1 commit 77d929f

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

main.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,8 +1909,31 @@ whisper_vocab::id whisper_sample_timestamp(
19091909
return probs_id[0].second;
19101910
}
19111911

1912+
// naive Discrete Fourier Transform
1913+
// input is real-valued
1914+
// output is complex-valued
1915+
void dft(const std::vector<float> & in, std::vector<float> & out) {
1916+
int N = in.size();
1917+
1918+
out.resize(N*2);
1919+
1920+
for (int k = 0; k < N; k++) {
1921+
float re = 0;
1922+
float im = 0;
1923+
1924+
for (int n = 0; n < N; n++) {
1925+
float angle = 2*M_PI*k*n/N;
1926+
re += in[n]*cos(angle);
1927+
im -= in[n]*sin(angle);
1928+
}
1929+
1930+
out[k*2 + 0] = re;
1931+
out[k*2 + 1] = im;
1932+
}
1933+
}
1934+
19121935
// Cooley-Tukey FFT
1913-
// poor man's implmentation - use something better
1936+
// poor man's implementation - use something better
19141937
// input is real-valued
19151938
// output is complex-valued
19161939
void fft(const std::vector<float> & in, std::vector<float> & out) {
@@ -1924,6 +1947,11 @@ void fft(const std::vector<float> & in, std::vector<float> & out) {
19241947
return;
19251948
}
19261949

1950+
if (N%2 == 1) {
1951+
dft(in, out);
1952+
return;
1953+
}
1954+
19271955
std::vector<float> even;
19281956
std::vector<float> odd;
19291957

@@ -2014,9 +2042,20 @@ bool log_mel_spectrogram(
20142042
// FFT -> mag^2
20152043
fft(fft_in, fft_out);
20162044

2017-
for (int j = 0; j < n_fft; j++) {
2045+
for (int j = 0; j < fft_size; j++) {
20182046
fft_out[j] = (fft_out[2*j + 0]*fft_out[2*j + 0] + fft_out[2*j + 1]*fft_out[2*j + 1]);
20192047
}
2048+
for (int j = 1; j < fft_size/2; j++) {
2049+
//if (i == 0) {
2050+
// printf("%d: %f %f\n", j, fft_out[j], fft_out[fft_size - j]);
2051+
//}
2052+
fft_out[j] += fft_out[fft_size - j];
2053+
}
2054+
if (i == 0) {
2055+
//for (int j = 0; j < fft_size; j++) {
2056+
// printf("%d: %e\n", j, fft_out[j]);
2057+
//}
2058+
}
20202059

20212060
// mel spectrogram
20222061
for (int j = 0; j < mel.n_mel; j++) {
@@ -2048,6 +2087,7 @@ bool log_mel_spectrogram(
20482087
mmax = mel.data[i];
20492088
}
20502089
}
2090+
//printf("%s: max = %f\n", __func__, mmax);
20512091

20522092
mmax -= 8.0;
20532093

0 commit comments

Comments
 (0)