Tuesday, October 30, 2018

signal synthesis - Generate loopable sine wave cycle for given frequencies


I am somewhat new to DSP so bare with me. I am working on a simple wavetable oscillator. What I am trying to do is generate one cycle of a wave form for each note (frequency) and store it in an array for later playback. I know I could generate one frequency, store it and resample to the other frequencies in real time but I'm looking for performance and I'm still learning so I'm trying to keep it simple for now.


In my first attempts I was able to generate one cycle of a sine wave at each frequency but when I went to play back the stored wave forms the frequencies were off by 0.5 - 0.10 hz or more and had a noticeable clicking noise when the cycle repeated. I know this is because the start and end of the wave form are not lining up on 0. I've been struggling to solve this problem.


Below is my most recent C# code I use to generate and store the sine wave. This will produce more cycles but seems to fix Hz variance I was getting with just generating one cycle. The only issue that remains is the clicking when it loops back. I may be going about this completely wrong so any suggestions would be greatly appreciated.


double frequency = 440.0; //A4
double sampleRate = 44100;
double numSamples = sampleRate / frequency;

double phase = frequency / sampleRate;

int j = 0;
for (double i = 0.0; i < numSamples; i += phase)
{
double sample = Math.Sin(i * 2 * Math.PI);

sineWaveTable[j] = sample;

j++;

}

I'm not sure if it's even possible to generate a single cycle of a sine wave for each note and have it start and stop on 0. If not, am I stuck with having to use resampleing to get the other desired notes?




No comments:

Post a Comment

periodic trends - Comparing radii in lithium, beryllium, magnesium, aluminium and sodium ions

Apparently the of last four, $\ce{Mg^2+}$ is closest in radius to $\ce{Li+}$. Is this true, and if so, why would a whole larger shell ($\ce{...