-4
$\begingroup$

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

$\endgroup$
10
  • $\begingroup$ I searched before asking... that answer is very technical... I don't quite know how to proceed from the statements in my question. $\endgroup$
    – Zibri
    Commented Sep 19, 2019 at 14:33
  • 1
    $\begingroup$ 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... $\endgroup$ Commented Sep 19, 2019 at 14:53
  • 2
    $\begingroup$ 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. $\endgroup$
    – MBaz
    Commented Sep 19, 2019 at 14:54
  • 1
    $\begingroup$ 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 $\endgroup$ Commented Sep 19, 2019 at 15:49
  • 1
    $\begingroup$ 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. $\endgroup$
    – Peter K.
    Commented Sep 19, 2019 at 15:58

1 Answer 1

3
$\begingroup$

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?

$\endgroup$
13
  • $\begingroup$ again... I don't want to filter! I wish to add N sine waves of increasing frequencies in the range and random amplitude. $\endgroup$
    – Zibri
    Commented Sep 19, 2019 at 15:45
  • 2
    $\begingroup$ @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. $\endgroup$
    – Peter K.
    Commented Sep 19, 2019 at 15:53
  • 3
    $\begingroup$ @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. $\endgroup$
    – user28715
    Commented Sep 19, 2019 at 16:15
  • $\begingroup$ 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. $\endgroup$
    – Zibri
    Commented Sep 22, 2019 at 9:21
  • 1
    $\begingroup$ @Zibri nobody said you couldn’t. We all asked why you would want to avoid filtering which you ignored $\endgroup$
    – user28715
    Commented Sep 22, 2019 at 11:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.