Is it possible to optmize computation keep sine/osc into normalized [0.0, 1.0] domain?

I’ve a code that process buffers of samples/audio data. Here’s the code:


#define PI 3.141592653589793238
#define TWOPI 6.283185307179586476

const int blockSize = 256;
const double mSampleRate = 44100.0;
const double mHostPitch = 2.0;
const double mRadiansPerSample = TWOPI / mSampleRate;

double mGainNormalizedValues[blockSize];
double mPitchNormalizedValues[blockSize];
double mOffsetNormalizedValues[blockSize];

class Oscillator
    double mPhase = 0.0;
    double minPitch = -48.0;
    double maxPitch = 48.0;
    double rangePitch = maxPitch - minPitch;
    double pitchPd = log(2.0) / 12.0;
    double minOffset = -900.0;
    double maxOffset = 900.0;    
    double rangeOffset = maxOffset - minOffset;

    Oscillator() { }

    void ProcessBuffer(double voiceFrequency, int blockSize, double *left, double *right) {
        // precomputed data
        double bp0 = voiceFrequency * mHostPitch;

        // process block
        for (int sampleIndex = 0; sampleIndex < blockSize; sampleIndex++) {
            double output = (sin(mPhase)) * mGainNormalizedValues[sampleIndex];

            *left++ += output;
            *right++ += output;

            // next phase
            mPhase += std::clamp(mRadiansPerSample * (bp0 * WarpPitch(mPitchNormalizedValues[sampleIndex])) + WarpOffset(mOffsetNormalizedValues[sampleIndex]), 0.0, PI);
            while (mPhase >= TWOPI) { mPhase -= TWOPI; }

    inline double WarpPitch(double normalizedValue) { return exp((minPitch + normalizedValue * rangePitch) * pitchPd); }
    inline double WarpOffset(double normalizedValue) { return minOffset + normalizedValue * rangeOffset; }    

int main(int argc, const char *argv[]) {
    int numBuffer = 1024;
    int counterBuffer = 0;    

    Oscillator oscillator;        

    // I fill the buffer often
    while (counterBuffer < numBuffer) {
        // init buffer
        double bufferLeft[blockSize];
        double bufferRight[blockSize];
        memset(&bufferLeft, 0, blockSize * sizeof(double));
        memset(&bufferRight, 0, blockSize * sizeof(double));

        // emulate params values for this buffer
        for(int i = 0; i < blockSize; i++) {
            mGainNormalizedValues[i] = i / (double)blockSize;
            mPitchNormalizedValues[i] = i / (double)blockSize;
            mOffsetNormalizedValues[i] = i / (double)blockSize;

        // process osc buffer
        oscillator.ProcessBuffer(130.81278, blockSize, &bufferLeft[0], &bufferRight[0]);

        // do somethings with buffer



  1. init a Oscillator object
  2. for each buffer, fill some param arrays with values (gain, pitch, offset); gain remain normalized [0.0, 1.0], while pitch and offset range in -48/48 and -900/900
  3. then I iterate the buffer, calculating the Oscillator's sine due to pitch and offset, and I apply a gain; later, I move the phase, incrementing it

The whole domain of operations are normalized [0.0, 1.0]. But when I need to manage pitch and offset, I need to switch domain and use different values (i.e. the Warp functions).

This required lots of computations and process. I'd like to avoid it, so I can improve the code and performances.

How would you do it? Can I keep into [0.0, 1.0]? Could I improve the performance?

scoring matrices computation from transition probability matrix

The transition probability matrix over time t is computed as P(t) = e ^Qt. In order to calculate a general set of transition probability matrices what type of t values can be used ( t = expected substitutions per site)? How can these transition probability matrices be converted to scoring matrices for use in homolog searches/alignment?

I am working with amino acids substitution models.

Thanks !!

2D DWT computation order

In 2D the discrete wavelet transform (DWT) of an image using lifting based 5/3 filter, if I perform a row-wise operation first then perform column-wise operation then I will get 4 sub-band LL, LH and HL and HH.

  • During reconstruction, that is IDWT, should I maintain the order in reverse direction that is column first and then row?
  • Or any order I can maintain?
  • In JPEG 2000 there is no order information found in the header. Then how I can main the order during reconstruction?

Runtime analysis of prime-set computation

I want to find the all primes between 2 and $k-1$. We can come up with an $O(klog k)$ running time algorithm. I want to compute this set in $O(k)$ running time. For this algorithm is :

enter image description here

we are maintaing a one pointer from node $i$ of the list $L$ to index $i$ of array $N$ and one pointer from index $i$ of $N$ to $i$th node of list $L$. We will use a doubly linked list in such a way that we mark each element of $N$ exacly once.

  1. Pick the smallest unmarked element $i ge 2$ in $N$ and add $i$ to $Q_{k-1}$.
  2. Starting from the first node, walk up the list $L$ in increasing order until we find the smallest number $r$ in it such that $rcdot i ge k-1$.
  3. Walk down the list $L$ (crucially, in decreasing order) from the node $r$, and for each $l$ in $L$, seen, mark the element $N[icdot l]$ and delete the node containing $icdot l$ from $L$ ( this node in $L$ is accessed through the pointer from the $(icdot l)$th location in $N$ ).
  4. If $N$ has any unmarked element $ge 2$, go back to first step

In the first round all the multiple of 2 will get marked. It is easy to verify that each element in the array will get marked just once.

Question : How to show that above algorithm has Running time $O(k)$?

After first iteration all multiples of 2 will be marked in the array $N$ and all multiples of 2 will be removed from the list $L$. In the second iteration multiples of 3 and so on.

text{Total time}: &= c_1k + c_2frac{k}{2} +c_3frac{k}{2 times 3} +c_4frac{k}{2 times 3 times 5} + c_5 frac{k}{2 times 3 times 5 times 7}+cdots\
&=k left(c_1 + c_2frac{1}{2} +c_3frac{1}{2 times 3} +c_4frac{1}{2 times 3 times 5} + c_5 frac{1}{2 times 3 times 5 times 7}+cdots right) \
&= O(k)

Reference : link

scoring matrices computation from transition probability matrix

The transition probability matrix over time t is computed as P(t) = e ^Qt. In order to calculate a general set of transition probability matrices what type of t values can be used ( t = expected substitutions per site). How these transition probability matrices can be converted to scoring matrices to use in homolog searches/alignment ??

I am working with amino acids substitution models !!

Thanks !!

Parallel Computation

How can I modify this code in order to run in parallel? (It’s some part of my main code)

LogNeg = {};
Do[LNeg = 0;
  A = {{CoMat[[2*i - 1, 2*i - 1]], 
    CoMat[[2*i - 1, 2*i]]}, {CoMat[[2*i, 2*i - 1]], 
    CoMat[[2*i, 2*i]]}};

  B = {{CoMat[[2*j - 1, 2*j - 1]], 
    CoMat[[2*j - 1, 2*j]]}, {CoMat[[2*j, 2*j - 1]], 
    CoMat[[2*j, 2*j]]}};

  F = {{CoMat[[2*i - 1, 2*j - 1]], 
    CoMat[[2*i - 1, 2*j]]}, {CoMat[[2*i, 2*j - 1]], 
    CoMat[[2*i, 2*j]]}};

  S = ArrayFlatten[{{A, F}, {Transpose[F], B}}];

  v = eta /. 
   Solve[eta^4 - (Det[A] + Det[B] - 2*Det[F])*eta^2 + Det[S] == 0];

  LNeg += -Log2[Min[Abs[v], 1]], {j, i + 1, M}], {i, 1, M}];

  AppendTo[LogNeg, LNeg], {t, 0, tMax}];

I’ve used ParallelDo[], but It doesn’t work.

Thank you in advance

computation of an integral for 2nd order non degenerate perturbation theory

I am given that the potential of a diatomic molecule is equal to $$V(rho)=-2V left ( frac{1}{rho ^2}-frac{1}{2 rho ^2} right ) $$ With $rho=r/a$ is a dimesionless coordinate, and $r$ is the separation distance between the two atoms. I found the first order corrections without issue, but I am stuck on finding the second order one. I know that
$$E_n^2=sum_{mneq n}frac{|langlepsi_m^0|H’|psi_n^0rangle|^2}{E_n^0-E_m^0}$$
For the given problem, I also found that the wavefunctions are $psi=e^{-x^2/2} H_{n}(x)$ (hermite polynomials). My problem is in computing the integral of the inner product, i.e, computing
$$langlepsi_m^0|H’|psi_n^0rangle=int_{-infty}^{infty} x^3 e^{-x^2} H_{n}(x)H_{m}(x)dx$$
I could apply integration by parts a bazillion times to obtain the answer, but it is far too tedious. From reading Griffiths, I know that there is a way to do this much simpler with dirac notation and ladder operators, in conjunction with the usual ladder operator identities, but I am unsure on how to go about this.

Anomaly in Curl Computation

Suppose computing the 3D curl of a vector field involves finding the determinant of this matrix…

enter image description here

According to the Indian method of computing determinants, we should be able to break this into the subdeterminants obtained by excluding any row or column of our choice. However, if we choose the right column, the determinant will incorrectly appear as $7y^3z^2(0) – xyz(0) + (-3x – z)(0)$ (where $0$ is the $0$ vector), which equals the $0$ vector. Choosing the left column is the sane route, while choosing the middle column appears to work, but choosing the right column leads to a different result. Why is this?