错误的IO操作顺序使用putStr和getLine(Wrong IO actions order using putStr and getLine)
我有以下代码:
main = do putStr "Test input : " content <- getLine putStrLn content
当我运行它(使用
runhaskell
)或编译它(ghc 6.10.4)时,结果如下所示:asd Test input : asd
为什么
Test input : asd
在asd
后打印asd
?在使用
putStr
http://learnyouahaskell.com/上的代码示例中,getLine
提供的输出与我的不同。 当我使用putStrLn
,程序按预期工作(打印,然后提示和打印)。它是
ghc
一个错误,还是它应该工作的方式?I have the following code:
main = do putStr "Test input : " content <- getLine putStrLn content
When I run it (with
runhaskell
) or compile it (ghc 6.10.4) the result is like this:asd Test input : asd
Why is
Test input : asd
being printed afterasd
?In the code sample on http://learnyouahaskell.com/, which uses
putStr
, thegetLine
's presented output is different than mine. When I useputStrLn
the program works as expected (print, then prompt, and print).Is it a bug in
ghc
, or it is the way that it should work?
原文:https://stackoverflow.com/questions/2500459
最满意答案
在已经有很多类似/相关的问题上,这些问题值得一读,因为答案中包含了大量有用的信息和建议,但实质上你需要这样做:
- 将音频数据转换为FFT所需的格式(例如int - > float,单独的L / R通道)
- 应用合适的窗口功能 (例如Hann aka Hanning窗口 )
- 应用FFT(注意:如果使用典型的复数到复数的FFT,则将输入数组的虚部设置为零)
- 计算第一个N / 2 FFT输出格的大小(
sqrt(re*re + im*im)
)- 可选将幅度转换为dB(log)标度(
20 * log10(magnitude)
)- 绘制N / 2(对数)幅度值
请注意,尽管FFTW是一种非常好且非常快的FFT,但对于初学者来说可能会有些压倒性的影响 - 如果您想将其作为商业产品的一部分包含在内,它也非常昂贵 - 我建议从KissFFT开始。
There are quite a few similar/related questions on SO already which are well worth reading as the answers contain a lot of useful information and advice, but in essence you need to do this:
- convert audio data to format required by FFT (e.g. int -> float, separate L/R channels)
- apply suitable window function (e.g. Hann aka Hanning window)
- apply FFT (NB: if using typical complex-to-complex FFT then set imaginary parts of input array to zero)
- calculate magnitude of first N/2 FFT output bins (
sqrt(re*re + im*im)
)- optionally convert magnitude to dB (log) scale (
20 * log10(magnitude)
)- plot N/2 (log) magnitude values
Note that while FFTW is a very good and very fast FFT it may be a little overwhelming for a beginner - it's also very expensive if you want to include it as part of a commercial product - I recommend starting with KissFFT instead.
相关问答
更多-
在已经有很多类似/相关的问题上,这些问题值得一读,因为答案中包含了大量有用的信息和建议,但实质上你需要这样做: 将音频数据转换为FFT所需的格式(例如int - > float,单独的L / R通道) 应用合适的窗口功能 (例如Hann aka Hanning窗口 ) 应用FFT(注意:如果使用典型的复数到复数的FFT,则将输入数组的虚部设置为零) 计算第一个N / 2 FFT输出格的大小( sqrt(re*re + im*im) ) 可选将幅度转换为dB(log)标度( 20 * log10(magnit ...
-
如果你的意思是音频频谱的频谱,你可以采取以下步骤: 将音频数据转换为原始音频格式,如16位PCM(如果使用流,可能在Windows中) 使用像JTransforms这样的快速傅里叶变换库来计算窗口上的频谱。 可视化包含光谱值的数组 If you mean by audio spectrum the frequency spectrum you could take these steps: Convert the audio data to a raw audio format like 16-Bit PC ...
-
对于单边FT而言,只需取FFT算法输出的前半部分即可。 考虑到你的输入是实值的,另一半(负值频率)是多余的。 1/8秒很长。 请注意,如果我记得正确(音乐不是我的专业),相关频率大约在160-1600赫兹之间。 那些将位于英国“金融时报”的最左侧区域。 您计算的最高频率(在丢弃FFT的右半部分后)是采样频率的一半,即44.1 / 2 kHz。 最低频率和样本之间的距离由变换长度(44.1 kHz /样本数)给出。 For a single-sided FT simply take the first hal ...
-
如果你想要绘制一些东西,你需要使用一个可以绘制图形或为另一个程序生成数据的库。 有很多选择 - 你可以使用的一些选项: Qt的 SFML SDL gnuplot的 C ++标准库不提供任何开箱即用的图形功能。 I converted the code and successfully plotted it using Gnuplot. The code is given below and was done in Visual Studio 2017 (hence the stdafx.h header f ...
-
真正的fft错误 - ffmpeg(Real fft error - ffmpeg)[2024-02-11]
在av_free之后调用av_free是不正确的 。 av_rdft_end已经释放了ctx指向的内存空间。 (此外, av_rdft_init后缺少分号。) 编辑: 第二个问题是operator new[]返回不正确对齐的指针。 请改用av_malloc 。 av_malloc分配一个适合所有内存访问(包括SSE指令)的对齐块。 Calling av_free after av_rdft_end is incorrect. av_rdft_end already frees the memory spac ... -
NAudio: http ://naudio.codeplex.com/(开源) Bass和Bass.Net:http://www.un4seen.com/(非商业免费) Fmod Ex: http ://www.fmod.org/index.html(也可免费用于非商业用途) 使用Bass做你需要的东西非常简单: string filepath =""; Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero, null); ...
-
如何在c#中创建音频频谱(How to create audio spectrum in c#)[2023-06-23]
有一篇CodeProject文章展示了如何创建一个可视化工具。 如果您想执行更高级的操作,您可以查看诸如FMOD和BASS等库。 他们都可以用几行代码来显示频谱。 但是,如果您的项目用于商业用途,则应阅读其许可证。 或者,您可以使用开源NAudio库,这也使得显示频谱更加容易。 包含示例代码,例如SpectrumAnalyser类。 There's a CodeProject article that shows how to create a visualizer. If you want to perf ... -
问题已在5.2版中修复 The problem has been fixed in version 5.2
-
感谢Severin的帮助,我最终按照本文中提到的指令将四元数FFT分成两个复杂到复杂的二维FFT,并成功地重现了本文所示的结果。 这样的事情:(请告诉我,如果我错了:)) #include
#include using namespace std; using namespace utils; using namespace Array; using namespace fftwpp; void SaliencyMapHandler::quaternionFou ...