简体   繁体   English

在编译时或运行时在C#中创建LUT

[英]Creating an LUT in C# at compile time or run time

I feel like this is a stupid question, but I can't think of a good way to do it. 我觉得这是一个愚蠢的问题,但我想不出一个好方法。

What I want to do is create a sine wave LUT at compile time or run time. 我要做的是在编译时或运行时创建一个正弦波LUT。 Ideally compile time, but run time is fine if it is much easier to code. 理想情况下是编译时,但如果编写起来容易得多,则运行时很好。 However, I want this static object to be accessible by everything that includes its library (I don't want to have to pass it). 但是,我希望包含它的库的所有内容都可以访问此静态对象(我不想通过它)。

I have a feeling that I will be changing the amplitude, the number of samples, the number of cycles (in between compiles, it will be set once the program is running), etc, so I don't want to have to generate the sine wave elsewhere and hard code the values. 我有一种感觉,我将要更改幅度,样本数,周期数(在两次编译之间,将在程序运行时进行设置),等等,所以我不想生成正弦波在其他地方并对值进行硬编码。

I want it to be static because I don't want to have to recreate the sine wave every time I need it. 我希望它是静态的,因为我不想每次需要时都必须重新创建正弦波。 The problem I am having is that I don't have a constructor to initialize it in and I am not sure how else to just make it run one time without passing it to objects or across a few different libraries. 我遇到的问题是我没有构造函数来对其进行初始化,而且我不确定如何使它一次运行而不将其传递给对象或跨几个不同的库。

I know this must be possible and probably very easy, but I just not sure where to look. 我知道这必须可行并且可能非常容易,但是我只是不确定在哪里看。 On top of that, it might just be a programming style problem as well so any suggestion would be welcomed. 最重要的是,它也可能只是一个编程风格的问题,因此任何建议都将受到欢迎。

Thanks 谢谢

  public static class Sines {
    private static double[] lut;

    static Sines() {
      lut = new double[2048];
      for (int ix = 0; ix < lut.Length; ++ix)
        lut[ix] = Math.Sin(Math.PI * 2 * ix / lut.Length);
    }

    public static double Lookup(int index) {
      return lut[index];
    }
  }

Usage: 用法:

double value = Sines.Lookup(1024);

It's not entirely clear what you'll want this wave to do. 目前尚不清楚您希望此波做什么。 Are you trying to get the value for any particular time, or is there something you want the wave to do as a whole ? 您是要在任何特定时间获取价值,还是要整体上做某件事?

Is there anything you need to do beyond calling Math.Sin with the right value (based on frequency and time) and then multiply it by the amplitude? 除了使用正确的值(基于频率和时间)调用Math.Sin然后将其乘以幅度外,您还有什么需要做的吗? Have you already tried that and found it's too slow if you do it every time you need it? 您是否已经尝试过,发现每次需要时都太慢了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM