# Math behind tone generator with Beats

1. Jun 3, 2014

### btb4198

ok this is an example:
Frequency = 180 Hz
Amplitude = 0.5 db
Beat = 4 Hz
waveform sampling rate is 44100 Hz

ok

1) Which the information above, does that mean that every 11025 samples there should be an beat ( meaning 0 s) ? The math I used was number for sample = (1D / Beat) / ( 1D/44100D)

2) if I solve for λ( wavelength) and make sure I do not start adding 0s before the λ starts again will that stop noise/ distortion in my tone ? I get ticks in my wave files, but if I do not interrupt the λ there should be no distortion or ticks right ?

3) From the information above λ should be (1/44100) / (180)
λ= 1.2597631645250692869740488788108e-7s
λ ( wavelength) = C/ F

2. Jun 3, 2014

### Baluncore

I am confused by the question, you have not explained your setup.

Are you using sine waves or logical 0 and 1 square waves?

You must differentiate between wavelength and period = 1 / frequency. Wavelength is a velocity and so requires a transmission medium. Period is just the time for one cycle.

If your waveform record contains multiples of one cycle, so the ends line up, it should not produce beats or clicks.

3. Jun 3, 2014

### btb4198

I am using an Sine wave.

f(t) = Amplitude * Sin(PI * Frequency * T / 44100D);

I am not using the period.
just the wavelength
λ ( wavelength) = C/ F
C in velocity
f is frequency

about the transmission medium... I am make a sound file to my medium would be air
how would I could up with my λ ?
my wavefile is set to 44100 Hz so I thought my C ( velocity would be (1/44100) s
is that not right ?

4. Jun 3, 2014

5. Jun 3, 2014

### btb4198

" 1) Which the information above, does that mean that every 11025 samples there should be an beat ( meaning 0 s) ? The math I used was number for sample = (1D / Beat) / ( 1D/44100D)"
Is this right ?

or is there another equation I should use?

6. Jun 4, 2014

### btb4198

ok I looked at those site
and i try to make some for the beats...
but how do i make a beat for 4 Hz that goes down to 0 ? and not just close to 0 ?
so like F(t) = 10 sin( π * 180 * t / 44100 ) + 5 sin( π * 184 * t/44100)
how different should the Amp how to be to get 0 ?

7. Jun 4, 2014

8. Jun 4, 2014

### btb4198

so what should the function be ?

9. Jun 4, 2014

### btb4198

how would the function look to make it go down to zero ?
does the two sinewave have to be 150 degrees out of phase ? to get 180 hz at a 4hz beat ?
I need it to drop down to 0

10. Jun 6, 2014

### btb4198

does anyone know how to ?

11. Jun 6, 2014

### Baluncore

I am still totally confused about what you are trying to do. At the moment I think that is because you are not explaining your requirement in technical terms with exact meanings.

Two sine waves, with different frequencies, will not have a fixed phase. Their phase difference will rotate continuously, that phase difference is a beat between the two waves.

What do you mean by “go to zero” ? What should go to zero ?
If you can draw and post a plot of the signal you want, someone here will give you an equation.

You are assuming the world is simple and that we can easily guess what you mean.
We know the world is complex and have no idea which answer of the 100s available you are wanting.

Maybe you should define the setup you are using and then ask one question. We will then question you about your meaning, then progressively revise your setup description and question until we understand each other. You will then have the answers you need.

12. Jun 7, 2014

### meBigGuy

What is your definition of a beat?

Can you write an equation for a modulated sine wave? AM Modulate a 1000 Hz sine wave with a 4 Hz sine wave (or triangle wave, or squarewave. This is multiplication.

Can you write equations for summing two sine waves of different frequencies and solve for the times when the sum is zero?

You need to precisely define the content you want. Is it a single sinewave (1 frequency) that varies in amplitude? Is it two frequencies that "beat" with each other? If it is two frequencies that "beat" at 4 hz, then they need to be 4 Hz apart in frequency (like when someone tunes a guitar by getting the "beat" to 0 Hz)

If it is 1 frequency that varies in amplitude, what is the precise definition of the amplitude controlling waveform.

You keep asking the same thing over and over without defining your terms.

13. Jun 7, 2014

### btb4198

I am tying to make an Isochronic tones. " are regular beats of a single tone used for brainwave entrainment. Similar to monaural beats, the interference pattern that produces the beat is outside the brain so headphones are not required for entrainment to be effective. They differ from monaural beats, which are constant sine wave pulses rather than entirely separate pulses of a single tone."

http://en.wikipedia.org/wiki/Isochronic_tones#mediaviewer/File:Isochronic-toes.svg

The picture make it look like it drop down to 0 during the beat. I am trying to get a to do that with out any tickles ... and I have trying fating in but that is not working... I still get a tick.

does anyone have an Isochronic algorithm?

I have been looking everywhere and I cant find one...
the only useful information I found on it, is from that wiki and as you can tell it is not that useful ...

14. Jun 7, 2014

### btb4198

15. Jun 7, 2014

### btb4198

Baluncore and meBigGuy do you understand what I am asking now ?

16. Jun 7, 2014

### Baluncore

Yes, you need bursts of a single frequency sine wave, so you multiply a sinewave by what looks like a trapezoidal window to get alternating bursts of tone and quiet. The ramp at the ends of the trapezoidal window prevent clicks being produced.

It is hard to write a window function in a single line, but a multi-line way is to use the positive part of a sinewave near y=0. If k is 1 then the pulses have a sinewave profile, as k increases, say 3, the trapezoidal window starts to appear and becomes steeper and more clicky with higher k values.

w(t) = k * sin( t * 2*Pi * 4Hz )
if w(t) < 0 then w(t) = 0
if w(t) > 1 then w(t) = 1
f(t) = w(t) * Amplitude * Sin(PI * Frequency * T / 44100D);

17. Jun 8, 2014

### meBigGuy

I don't understand what you find difficult about modulating a sine wave with another waveform. You have been at this for a long time.

Just modulate a 100Hz sine wave with a 1 hz sine wave. Can you write the basic equation for that (multiplying 2 sine waves). Then debug that. You know exactly what it should look like.

Generate a 100 Hz sine wave. Look at it.
Generate a 1 Hz sine wave. Look at it.
Multiply them together. Look at it.

I don't understand how you can say "it ticks" and expect people to figure out what that means. There are all sorts of reasons it might do something that you call a tick. LOOK AT THE WAVEFORM AND FIGURE OUT WHAT YOU ARE DOING THAT MAKES IT DIFFERENT THAN WHAT YOU EXPECT TO SEE!

Once you can modulate a sine wave, you can change the modulating signal to anything you wish. It that signal "ticks" then look at it and try to figure out what is wrong with the waveform. Many things could go wrong that would cause a tick. Fast slopes, missing samples, arithmetic overflow, etc. Look at the waveform and debug your code.

18. Jun 8, 2014

### btb4198

ok I change it like you said
I have to change a few things like I added /44100 to W(t)
I did that because with out it I keep getting 0 and nothing would play
and I remove the 2... I have not had good lucky with adding 2s there.. this seem to double the Frequencies ...
so what do you think?
this is what i got:

Code (Text):

[CODE]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
using NAudio;

namespace Isochronic_tones_generator
{
class isochronicTone : WaveProvider32
{
public isochronicTone()
{
Frequency = 1000;
Amplitude = 0.25f;
Beat = 0;
n1 = 0;
}
int sample;
static int n1;
public float Frequency { get; set; }
public float Amplitude { get; set; }
public float Beat { get; set; }
public override int Read(float[] buffer, int offset, int sampleCount)
{
int sampleRate = WaveFormat.SampleRate;
for (int n = 0; n < sampleCount; n++)
{
float w = (float)(3D * Math.Sin( n1  * Math.PI * Beat /44100D ));
n1++;
if (w < 0)
{
w = 0;
}
if(w > 1)
{
w = 1;
}

float temp = (float) (w * Amplitude * Math.Sin(Math.PI * Frequency * n / 44100D));
buffer[n + offset] = temp;
sample++;
if (sample >= sampleRate) sample = 0;
}
return sampleCount;

}

}
}

[/CODE]

19. Jun 8, 2014

### Baluncore

I can't tell from your code what the signal looks or sounds like. Have you tried it ?

Since angular frequency, w, in radians per second is needed by the Sin(w) function, you will need to use w = (2 * Pi * freq_Hz * time / samples_per_sec) to correct the frequency from rad/sec to Hz.

The division by your sample rate is needed to interpolate the data points into the sample record.

Code (Text):
Const Double TwoPi = 8 * Atn(1)
Integer sps = 44100  ' sample rate
Double Hz = 44      ' sine wave frequency
Double beat = 4      ' frequency of pulse envelope
Double k = 3         ' trapezoidal
Double w             ' window value
Double theta         ' precompute phase in sample record
Double a(0 To sps-1) ' data array for one second
Integer t            ' time

For t = 0 To sps - 1
theta = TwoPi * t / sps
w = k * Sin(theta * beat)
If (w < 0) Then w = 0
If (w > 1) Then w = 1
a(t) = w * Sin(theta * Hz)
Next t
See the attached is a 44 Hz tone in a 4 Hz envelope

#### Attached Files:

• ###### Isochronic.png
File size:
31.4 KB
Views:
165
20. Jun 8, 2014

### btb4198

21. Jun 8, 2014

### Baluncore

Yes, but are you happy with it ?

22. Jun 9, 2014

### btb4198

Baluncore
yes, it sounds like the other file I posted. so that is good. sorry I am new to this...
that last code you posted, is that for matlab?
also I am not understanding " Since angular frequency, w, in radians per second is needed by the Sin(w) function, you will need to use w = (2 * Pi * freq_Hz * time / samples_per_sec) to correct the frequency from rad/sec to Hz.?

23. Jun 9, 2014

### btb4198

Baluncore,

also how did you know it should be a trapezoidal waveform ?
and how did you come up with your code ?

24. Jun 9, 2014

### Baluncore

The original problem with understanding your requirement was because you referred to a “beat” as generated by the addition of two sinewaves, rather than the product of one sinewave by an envelope function.

The sine() and cosine() functions repeat every 360° or 2*Pi “radians”. That is one cycle.
One hertz, Hz, = one cycle per second, = 2 * Pi radians per second.

Notice that I get four bursts of tone per second for Hz = 4 in my code, the frequency is calibrated. I suspect you may be out by a factor of two, which is one musical octave. That may be because you did not initially understand mathematical angular measure, where 2*Pi radians = 360°.

The code I posted was not in any particular language, it was a way of writing a mathematical algorithm, in a cross between BASIC and FORTRAN. I do not like C { }.

Experience. It must be a slow rise-time trapezoidal envelope to reduce the clicking sound of high frequencies present in a sudden step. I used part of a sine wave for the envelope because it was the easiest quick fix that would give control of the burst rate and envelope. There are other slightly better sounding solutions. I could see what was required by looking at your wiki link in post #13. I have been receiving and analysing RF signals that look like that for 35 years. My code was minimised because I avoided the complexities of implementation. I presented just enough code to show how the math works.

25. Jun 9, 2014

### btb4198

Baluncore,

are you saying that code was not the best way to go, but just the easiest way to explain it? what would be the best way? 35 years wow...
what do you mean by " I used part of a sine wave for the envelope " ?
how are you defining envelope?