-4

If I have this function:

while (true)
{
    frequency = 8000;
    wave = sin(sampleNumber*2*PI*frequency/sampleRate)
    sampleNumber++;
}

it will produce an 8kHz sine wave.

For white noise I have:

noise = 1-2*rand()/RAND_MAX;  // produces random numbers between -1 and 1

The white noise is in all frequencies obviously.

How can I have noise only in a specified range of frequencies (freq1 and freq2) ?

Note: I don't want to filter white noise in the range freq1-freq2 i asked how to PRODUCE it in that range.. (perhaps adding N sines of 1Hz with random amplitudes in the range?)

Zibri
  • 99
  • 4
  • I searched before asking... that answer is very technical... I don't quite know how to proceed from the statements in my question. – Zibri Sep 19 '19 at 14:33
  • and also.. I don't want to filter white noise in the range freq1-freq2 i asked how to PRODUCE it in that range.. – Zibri Sep 19 '19 at 14:51
  • 1
    Sorry if I understood you wrong, but isn’t „generation of band limited“ equal to produce ? In the answer they offer an approach to „produce“ it via filtering white noise... – Irreducible Sep 19 '19 at 14:53
  • 2
    BTW, your first code sample is wrong, it will produce a scalar, not a sine wave. Your code to generate noise is wrong too; you need randn to produce white noise. Finally: the way to produce noise in a frequency range is by filtering. – MBaz Sep 19 '19 at 14:54
  • @Irreducible no..let's say I want to "dirt" a tile on my floor (my question). what everyone does is to dirty all the house and then clean everything except the tile. I want to dirty ONLY that tile. – Zibri Sep 19 '19 at 15:47
  • @MBaz wrong? why? – Zibri Sep 19 '19 at 15:48
  • 1
    The uncomfortable answer however is, first dirt everything and clean up what you don’t want. And once this is done you can start to do what you wanted – Irreducible Sep 19 '19 at 15:49
  • @Irreducible I bet there is a way to only dirt the tile. And that was what my question was about. – Zibri Sep 19 '19 at 15:54
  • 1
    Closing because this question (even doing it without filtering) already has an explicit answer on the duplicate suggested by @Irreducible. See here or the link that it gives here. – Peter K. Sep 19 '19 at 15:58
  • you are all damn wrong. it CAN be generated without filtering... and here is prrof: I generated this as white noise at 16000Hz and 2kHz of bandwidth: https://prnt.sc/p9chhq no filtering involved. – Zibri Sep 22 '19 at 09:21

1 Answers1

3

i would suggest you use randn() instead of rand().

The most straightforward way to produce band limited noise is to filter white noise.

you could conceivably use a Gibbs sampler but that would be less efficient and require knowing how to set up the problem.

Could you explain why you are making a distinction?

  • again... I don't want to filter! I wish to add N sine waves of increasing frequencies in the range and random amplitude. – Zibri Sep 19 '19 at 15:45
  • 2
    @Zibri : Can you explain why you don't want to filter in your question? That might lead to answers that are more acceptable to you. – Peter K. Sep 19 '19 at 15:53
  • 3
    @Zibri as the Rolling Stones have song goes , you don’t always get what you want, but if you try some times, you get what you need. you could generate band limited noise by adding a set of random sines but it remains to be understood why that would be preferred. The standard way is to filter and there are statistical tests that demonstrate that the validity of that procedure. –  Sep 19 '19 at 16:15
  • you are all damn wrong. it CAN be generated without filtering... and here is prrof: I generated this as white noise at 16000Hz and 2kHz of bandwidth: https://prnt.sc/p9chhq no filtering involved. – Zibri Sep 22 '19 at 09:21
  • 1
    @Zibri nobody said you couldn’t. We all asked why you would want to avoid filtering which you ignored –  Sep 22 '19 at 11:58
  • @StanleyPawlukiewicz The reason is simple, while the making and filtering is preferable in electronics, in software the minimal code and minimal memory and resources impact the better. So filtering is a huge work compared to generating the right thing. – Zibri Sep 23 '19 at 14:30
  • False False False. You have not generated the right thing. Your noise is not flat across the band. you ignore the effort of calculating sines on the fly. Every DSP library has an optimized filter routine and optimized FFT. If a filter is prohibitive, you picked the wrong processor. –  Sep 23 '19 at 14:41
  • Perhaps but I feel like nobody here knows how to write good code. Saying that a biquad filter is as fast as this is just ignorant. When needing realtime sound generation expecially on microcontrollers you need the shortest and fastest code. Today it seems nobody can do anything without using some huge library or some huge all purposes routine. My question was specific and nobody answered. You just tried to change the question to make it fit with your apparently limited knowledge in programming and maybe even math. – Zibri Sep 28 '19 at 06:44
  • @Zibri you’re a hacker, not a programmer and i mean it in the negative way –  Sep 28 '19 at 14:51
  • @StanleyPawlukiewicz you can mean it as you want. But facts are facts: https://www.youtube.com/watch?v=Z8qqcSIJcng – Zibri Sep 29 '19 at 07:27
  • irrelevant. if you want to show that your code is faster than calling a biquad filter, profile it and post the results of the comparison. lame analogies and an unrelated video don’t prove anything. numbers are proof. what’s next, a cosplay video? –  Sep 29 '19 at 11:36
  • relevant facts are 1) you link to a math library to use the sine function 2) you ignore the computational load of calling that routine with arbitrary frequencies 3) you have poor spectral control 4) you don’t consider the run time scaling when you go from n samples to 2n,3n,4n.... samples. 5) you ignore the saturation/clipping problem for a fixed point arithmetic implementation. do i need to continue? are you going to call these facts ignorant? are you going to post a video with a donkey to address these issues? are you going to grow up? –  Sep 29 '19 at 12:31
  • Nope. I won't be teaching your majesty how to code. Bye. Have fun. – Zibri Sep 30 '19 at 06:42