如何将一组离散数据传输到频域并返回(最好是无损)
我想获取大约大小为 70-80k 的字节数组,并将它们从时域转换到频域(可能使用 DFT)。到目前为止,我一直在关注 wiki 并获得了此代码。
for (int k = 0; k < windows.length; k++) {
double imag = 0.0;
double real = 0.0;
for (int n = 0; n < data.length; n++) {
double val = (data[n])
* Math.exp(-2.0 * Math.PI * n * k / data.length)
/ 128;
imag += Math.cos(val);
real += Math.sin(val);
}
windows[k] = Math.sqrt(imag * imag + real
* real);
}
据我所知,这可以找到每个频率窗口/箱的幅度。然后我穿过窗户找到震级最高的那扇。我向该频率添加了一个标志,以便在重建信号时使用。我检查重建的信号是否与我的原始数据集匹配。如果它没有找到下一个最高频率窗口,并在重建信号时标记要使用的窗口。
这是我用于重建信号的代码,我最确定它是非常错误的(它应该执行 IDFT):
for (int n = 0; n < data.length; n++) {
double imag = 0.0;
double real = 0.0;
sinValue[n] = 0;
for (int k = 0; k < freqUsed.length; k++) {
if (freqUsed[k]) {
double val = (windows[k] * Math.exp(2.0 * Math.PI * n
* k / data.length));
imag += Math.cos(val);
real += Math.sin(val);
}
}
sinValue[n] = imag* imag + real * real;
sinValue[n] /= data.length;
newData[n] = (byte) (127 * sinValue[n]);
}
freqUsed 是一个布尔数组,用于标记重建信号时是否应使用频率窗口。
无论如何,出现的问题如下:
- 即使使用所有频率窗口,信号也不会重建。这可能是由于以下事实:...
- 有时 Math.exp() 的值太高,因此返回无穷大。这使得很难获得准确的计算。
- 虽然我一直遵循 wiki 作为指导,但很难判断我的数据是否有意义。这使得测试和识别问题变得困难。
脱离问题:
我对此相当陌生,并不完全理解一切。因此,任何帮助或见解都是值得赞赏的。感谢您花时间阅读所有内容,并提前感谢您可以提供的任何帮助。任何帮助真的会很好,即使你认为我正在以最糟糕的方式做这件事,我想知道。再次感谢。
-
编辑:
所以我更新了我的代码,使其看起来像:
for (int k = 0; k < windows.length; k++) {
double imag = 0.0;
double real = 0.0;
for (int n = 0; n < data.length; n++) {
double val = (-2.0 * Math.PI * n * k / data.length);
imag += data[n]*-Math.sin(val);
real += data[n]*Math.cos(val);
}
windows[k] = Math.sqrt(imag * imag + real
* real);
}
对于原始变换和:
for (int n = 0; n < data.length; n++) {
double imag = 0.0;
double real = 0.0;
sinValue[n] = 0;
for (int k = 0; k < freqUsed.length; k++) {
if (freqUsed[k]) {
double val = (2.0 * Math.PI * n
* k / data.length);
imag += windows[k]*-Math.sin(val);
real += windows[k]*Math.cos(val);
}
}
sinValue[n] = Math.sqrt(imag* imag + real * real);
sinValue[n] /= data.length;
newData[n] = (byte) (Math.floor(sinValue[n]));
}
对于逆变换。尽管我仍然担心它不能正常工作。我生成了一个包含单个正弦波的数组,它甚至无法分解/重建它。对我所缺少的有什么见解吗?
I would like to take an array of bytes of roughly size 70-80k and transform them from the time domain to the frequency domain (probably using a DFT). I have been following wiki and gotten this code so far.
for (int k = 0; k < windows.length; k++) {
double imag = 0.0;
double real = 0.0;
for (int n = 0; n < data.length; n++) {
double val = (data[n])
* Math.exp(-2.0 * Math.PI * n * k / data.length)
/ 128;
imag += Math.cos(val);
real += Math.sin(val);
}
windows[k] = Math.sqrt(imag * imag + real
* real);
}
and as far as I know, that finds the magnitude of each frequency window/bin. I then go through the windows and find the one with the highest magnitude. I add a flag to that frequency to be used when reconstructing the signal. I check to see if the reconstructed signal matches my original data set. If it doesn't find the next highest frequency window and flag that to be used when reconstructing the signal.
Here is the code I have for reconstructing the signal which I'm mostly certain is very wrong (it is supposed to perform an IDFT):
for (int n = 0; n < data.length; n++) {
double imag = 0.0;
double real = 0.0;
sinValue[n] = 0;
for (int k = 0; k < freqUsed.length; k++) {
if (freqUsed[k]) {
double val = (windows[k] * Math.exp(2.0 * Math.PI * n
* k / data.length));
imag += Math.cos(val);
real += Math.sin(val);
}
}
sinValue[n] = imag* imag + real * real;
sinValue[n] /= data.length;
newData[n] = (byte) (127 * sinValue[n]);
}
freqUsed is a boolean array used to mark whether or not a frequency window should be used when reconstructing the signal.
Anyway, here are the problems that arise:
- Even if all of the frequency windows are used, the signal is not reconstructed. This may be due to the fact that ...
- Sometimes the value of Math.exp() is too high and thus returns infinity. This makes it difficult to get accurate calculations.
- While I have been following wiki as a guide, it is hard to tell whether or not my data is meaningful. This makes it hard to test and identify problems.
Off hand from the problem:
I am fairly new to this and do not fully understand everything. Thus, any help or insight is appreciated. Thanks for even taking the time to read all of that and thanks ahead of time for any help you can provide. Any help really would be good, even if you think I'm doing this the most worst awful way possible, I'd like to know. Thanks again.
-
EDIT:
So I updated my code to look like:
for (int k = 0; k < windows.length; k++) {
double imag = 0.0;
double real = 0.0;
for (int n = 0; n < data.length; n++) {
double val = (-2.0 * Math.PI * n * k / data.length);
imag += data[n]*-Math.sin(val);
real += data[n]*Math.cos(val);
}
windows[k] = Math.sqrt(imag * imag + real
* real);
}
for the original transform and :
for (int n = 0; n < data.length; n++) {
double imag = 0.0;
double real = 0.0;
sinValue[n] = 0;
for (int k = 0; k < freqUsed.length; k++) {
if (freqUsed[k]) {
double val = (2.0 * Math.PI * n
* k / data.length);
imag += windows[k]*-Math.sin(val);
real += windows[k]*Math.cos(val);
}
}
sinValue[n] = Math.sqrt(imag* imag + real * real);
sinValue[n] /= data.length;
newData[n] = (byte) (Math.floor(sinValue[n]));
}
for the inverse transform. Though I am still concerned that it isn't quite working correctly. I generated an array holding a single sine wave and it can't even decompose/reconstruct that. Any insight as to what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您的代码(DFT 和 IDFT)已损坏。您对如何使用指数的问题感到困惑。 DFT 可以写成:
其中
j
是sqrt(-1)。这可以表示为:又可以分为:
如果您的输入数据是纯实数,则可以简化为:
因此总而言之,您不需要同时使用
exp
和余弦/正弦
。Yes, your code (for both DFT and IDFT) is broken. You are confusing the issue of how to use the exponential. The DFT can be written as:
where
j
is sqrt(-1). That can be expressed as:which in turn can be split into:
If your input data is real-only, this simplifies to:
So in summary, you don't need both the
exp
and thecos/sin
.除了@Oli正确提出的观点之外,您还对时域和频域之间的转换存在根本性误解。您的真实输入信号变成频域中的复杂信号。您不应该获取该值的大小并转换回时域(如果正确完成,这实际上会给您时域自相关,但这不是您想要的)。如果您希望能够重建时域信号,那么您必须保持复数频域信号不变(即单独的实数/虚数分量),并执行复数到实数 IDFT 以返回时域。
例如,您的正向变换应如下所示:
其中
windows
被定义为复数值数组。As well as the points that @Oli correctly makes, you also have a fundamental misunderstanding about conversion between time and frequency domains. Your real input signal becomes a complex signal in the frequency domain. You should not be taking the magnitude of this and converting back to the time domain (this will actually give you the time domain autocorrelation if done correctly, but this is not what you want). If you want to be able to reconstruct the time domain signal then you must keep the complex frequency domain signal as it is (i.e. separate real/imaginary components) and do a complex-to-real IDFT to get back to the time domain.
E.g. your forward transform should look something like this:
where
windows
is defined as an array of complex values.