生成 DFT 方波
我正在做一项作业,以 20kHz 的采样频率对频率为 500Hz 的方波执行 200 点 DFT,其幅度在 0 和 20 之间交替。
我正在使用 C++,并且我已经弄清楚如何编写 DFT 方程,我的问题是我无法使用 for 循环在代码中表示方波。
我真正仍然困惑的是这个方波在我的 200 点样本中有多少个周期。
谢谢
I'm working on an assignment to perform a 200 point DFT at a sampling frequency of 20kHz on a square wave of frequency 500Hz whose amplitude alternates between 0 and 20.
I'm using C++ and I have figured how to code the DFT equation, my problem is I'm having trouble representing the square wave in code using a for loop.
What I'm really still confused about is how many cycles of this square wave will be in my 200 point sample.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方波的周期为
20000/500=40
点,因此在 200 点样本中将恰好有 5 个方波周期 (200/40=5
代码>)。The period of the square wave is
20000/500=40
points, so you'll have exactly 5 periods of the square wave in your 200-point sample (200/40=5
).方波的一个周期将需要 1/500 秒。每个样本将为 1/20000 秒。一个简单的除法应该可以告诉您每个方波有多少个样本。
另一个部门会告诉您 200 点窗口中适合多少个波浪。
One cycle of your square wave will take 1/500 seconds. Each sample will be 1/20000 seconds. A simple division should tell you how many samples each square wave will be.
Another division will tell you how many of those waves will fit in your 200 point window.
如果您的采样频率为 20,000 Hz,并且您有一个频率为 500 Hz 的方波,这基本上意味着您每秒将有 500 个波周期,这意味着每 20,000 个样本将有 500 个周期。这意味着每个波周期需要 40 个样本(或点),因此如果您有 200 个点,则意味着您的 DFT 中应该有 5 个方波周期。
If your sampling frequency is 20,000 Hz and you have a square wave of frequency 500 Hz, this basically means that you will have 500 cycles of your wave per second, which means you will have 500 cycles in every 20,000 samples. This means that each wave cycle requires 40 samples (or points), so if you have 200 points that means you should have 5 square wave cycles within your DFT.
您可以通过在计算中包含单位来确保计算正确。因此,周期的量纲为时间,赫兹的量纲为 1.0/次,样本的量纲为无量纲。
通过编程方式,您可以使用 boost.units 来完成此操作。它会在编译时检查你的单位,如果你犯了错误,就会给你一个错误。
它还将阻止您的用户在代码中输入错误的单位。例如,通过输入 20 而不是 20000 作为频率(认为您正在以 kHz 为单位进行测量),
您的界面将类似于
用户必须输入以秒为单位的时间,
You can make sure you do your calculation right by including the units in your calculation. So the period has the dimension time, Hertz has the dimension of 1.0/time and samples is dimensionless.
Programatically, you can do this with boost.units. It will check your units at compile time and give you an error if you make a mistake.
It will also stop your user from entering the wrong units into your code. For example, by entering 20 instead of 20000 for the frequency (thinking you were measuring in kHz)
Your interface will then be something like
The user will have to enter the time in seconds,