File: FFTSpeed.cpp

package info (click to toggle)
linwsjt 0.4.7-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,356 kB
  • ctags: 1,896
  • sloc: cpp: 13,475; makefile: 417
file content (127 lines) | stat: -rw-r--r-- 3,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *   Copyright (C) 2002,2003 by Jonathan Naylor G4KLX/HB9DRD
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <sys/time.h>
#include <math.h>

#include "common/FFT.h"
#include "common/SFFT.h"
#include "common/SoundFile.h"

#include "FFTSpeed.h"

int main(int argc, char **argv)
{
	if (argc < 6) {
		::fprintf(stderr, "Usage: FFTSpeed <filename> <overlap> <fft length> <sfft first> <sfft last>\n");
		return 1;
	}

	wxString fileName = wxString(argv[1]);
	int       overlap = ::atoi(argv[2]);
	int     fftLength = ::atoi(argv[3]);
	int     sfftFirst = ::atoi(argv[4]);
	int      sfftLast = ::atoi(argv[5]);

	CFFTSpeed speed(fileName, overlap, fftLength, sfftFirst, sfftLast);
	speed.run();

	return 0;
}

CFFTSpeed::CFFTSpeed(const wxString& fileName, int overlap, int fftLength, int sfftFirst, int sfftLast) :
m_fileName(fileName),
m_overlap(overlap),
m_fftLength(fftLength),
m_sfftFirst(sfftFirst),
m_sfftLast(sfftLast)
{
}

CFFTSpeed::~CFFTSpeed()
{
}

void CFFTSpeed::run()
{
	CSoundDev* file = new CSoundFile;

	file->openRead(m_fileName, 11025, 16);

	double* audio = new double[30 * 11025];

	int length = 0;
	while (length < (30 * 11025)) {
		int len = 4096;

		if (!file->read(audio + length, len))
			break;

		length += len;
	}

	file->close();
	delete file;

	processFFT(audio, length);

	processSFFT(audio, length);

	delete[] audio;
}

void CFFTSpeed::processSFFT(double* audio, int length) const
{
	struct timeval tvBegin;
	::gettimeofday(&tvBegin, NULL);

	CSFFT sfft(m_fftLength, m_sfftFirst, m_sfftLast + 1);

	for (int i = 0; i < (length - m_fftLength); i++)
		sfft.process(audio[i]);

	struct timeval tvEnd;
	::gettimeofday(&tvEnd, NULL);

//	::printf("SFFT: start at %ld:%ld end at %ld:%ld\n", tvBegin.tv_sec, tvBegin.tv_usec, tvEnd.tv_sec, tvEnd.tv_usec);

	::printf("SFFT: elapsed: %ld seconds\n", tvEnd.tv_sec - tvBegin.tv_sec);
}

void CFFTSpeed::processFFT(double* audio, int length) const
{
	struct timeval tvBegin;
	::gettimeofday(&tvBegin, NULL);

	CFFT fft(m_fftLength);

	int done = 0;

	for (int i = 0; i < (length - m_fftLength); i += m_overlap) {
		done++;
		fft.process(audio + i);
	}

	struct timeval tvEnd;
	::gettimeofday(&tvEnd, NULL);

//	::printf("FFT: start at %ld:%ld end at %ld:%ld\n", tvBegin.tv_sec, tvBegin.tv_usec, tvEnd.tv_sec, tvEnd.tv_usec);

	::printf("FFT: elapsed: %ld seconds\n", tvEnd.tv_sec - tvBegin.tv_sec);
	::printf("FFT: performed %d FFTs\n", done);
}