识别/生成波形?

发布于 2024-12-04 10:56:49 字数 167 浏览 1 评论 0原文

我想编写一些代码,可以接受某种输入并将其识别为方波、三角波或某种波形。我还需要某种产生所述波的方法。

我确实有 C/C++ 经验,但是,我不确定如何模拟所有这些。最终,我想将其转换为微控制器程序,用于读取其模拟输入以确定波形。

编辑:抱歉;我应该提到它的频率是已知的,而幅度应该是未知的。

I would like to code something that could take some sort of input and identify it as a square wave, triangle wave, or some sort of waveform. I also need some way of generating said waves.

I do have experience with C/C++, however, I'm not sure how I would approach simulating all of this. Eventually, I would like to translate it to a microcontroller program for reading its analog input to determine the waveform.

EDIT: Sorry; I should have mentioned it would be at a known frequency and the amplitude should be unknown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸢与 2024-12-11 10:56:49

产生波比识别它们要容易得多。我有一个小型项目,可以产生一些波浪。这是一个示例,来自

    float amplitude;
    switch (sound->wavetype)
    {
        case LA_SQUARE:
            amplitude = sound->theta > .5 ? 1.0 : -1.0;
            break;
        case LA_SINE:
            amplitude = sin(2 * PI * sound->theta);
            break;
        case LA_TRIANGLE:
            amplitude = sound->theta > .5 ? 4 * sound->theta - 3 : -4 * sound->theta + 1;
            break;
        case LA_SAWTOOTH:
            amplitude = 2 * sound->theta - 1.0;
            break;
        case LA_NOISE:
            amplitude = ((float)rand() / RAND_MAX);
            break;
        default:
            ;
    }

” 此处在波浪形式沿每个帧进行更新,并取决于您要创建的波浪的频率。

至于识别波浪,如果您知道自己只会变得简单,未混合的正方形,三角形或正弦波,那么您可能只需进行一些简单的测试即可。查看沿波浪的任意两个点的振幅变化。如果它们是一样的,则是方波。如果它们线性更改(也就是说,如果振幅的变化是恒定的),那么您将有一个三角波(或锯齿状,如果您进行区分)。否则,这是一个正弦波。请记住,仅当您只期待这些类型的浪潮而没有混合或任何东西时,此检查才能起作用。我可以想到的还有其他一些边缘案例,但我会让您担心。

如果您正在做任何更典型的事情,那么您可能需要查找一本专门从事这种事情的书,就像评论部分中的建议一样。

Generating the waves is significantly easier than identifying them. I have a small project that does some wave generation. Here's an example from my project:

    float amplitude;
    switch (sound->wavetype)
    {
        case LA_SQUARE:
            amplitude = sound->theta > .5 ? 1.0 : -1.0;
            break;
        case LA_SINE:
            amplitude = sin(2 * PI * sound->theta);
            break;
        case LA_TRIANGLE:
            amplitude = sound->theta > .5 ? 4 * sound->theta - 3 : -4 * sound->theta + 1;
            break;
        case LA_SAWTOOTH:
            amplitude = 2 * sound->theta - 1.0;
            break;
        case LA_NOISE:
            amplitude = ((float)rand() / RAND_MAX);
            break;
        default:
            ;
    }

theta here is updated at every frame along the wave form and is dependent on the frequency of the wave you're creating.

As for identifying waves, if you know you're just going to be getting simple, unmixed square, triangle or sine waves, you can probably just do some simple tests. Look at the change in amplitude at any two points along the wave. If they're the same, square wave. If they're changing linearly (that is, if the change in amplitude is constant) you've got a triangle wave (or a sawtooth, if you're making that distinction). Otherwise, it's a sine wave. Keep in mind this check only works if you're expecting just those types of wave, and they're not being mixed or anything. There's some other edge cases in there I can think of but I'll let you worry about that.

If you're doing anything fancier, you're going to need to probably look up a book that specializes in this sort of thing, like the one suggested in the comments section.

想挽留 2024-12-11 10:56:49

从 MATLAB 或免费的 GNU Octave 开始,工作方式类似。您可以生成具有所需波形的数组并编写适当的函数来解码/识别。当您弄清楚细节后,获取 FFTW(西方最快的傅里叶变换)库的副本来处理您的 c/c++ 代码的 fft/ifft 例程。 MATLAB 的信号处理工作台模块有许多有用的工具来实现您的目标。

Start with MATLAB or the free GNU Octave work-alike. You can generate arrays with the desired waveforms and write appropriate functions to decode/identify. When you have the details worked out, grab a copy of the FFTW (fastest fourier transforn in the west) library to handle the fft/ifft routines for your c/c++ code. The signal processing workbench module of MATLAB has lots of useful tools to achieve your objective.

清欢 2024-12-11 10:56:49

识别波形:如果您知道频率,您可以通过使用离散梯度做很多事情,正如 Alex 在他的回答中所建议的那样。

另一种方法是使用插值技术并查看系数。还有一种是快速傅立叶变换。后两者的计算量更大,但也更准确,特别是在识别更复杂的波形时。您必须查看您的 uC 是否足够快,或者如果幸运的话,有硬件 FFT。

To identifying waveforms: If you know the frequency, you can do quite a lot by using discrete gradients, as Alex suggests in his answer.

Another method would be to use an interpolation technique and have a look at the coefficients. Still another would be a fast Fourier transform. These last two are computationally more intense, but also much more accurate, especially when identifying more complex waveforms. You would have to see whether your uC is fast enough or, if you are lucky, has a hardware-FFT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文