# White noise in range of frequencies [duplicate]

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?)

## marked as duplicate by MBaz, lennon310, Peter K.♦Sep 19 at 15:56

• 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 at 14:33
• 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 at 14:53
• 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 at 14:54
• 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 at 15:49
• 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 at 15:58

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 at 15:45
• @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 at 15:53
• @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. – Stanley Pawlukiewicz Sep 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: prnt.sc/p9chhq no filtering involved. – Zibri Sep 22 at 9:21
• @Zibri nobody said you couldn’t. We all asked why you would want to avoid filtering which you ignored – Stanley Pawlukiewicz Sep 22 at 11:58