
1. Re: Multiple Waveform Generation at the Same Time
user_78878863 Mar 18, 2016 2:31 AM (in response to linghe.wang)It does not really help when you post the same question to multiple, unrelateds forums (your question is neither a "known problem" nor is it related to "device programming").
Answer should be handled here: http://www.cypress.com/forum/psoc5architecture/questionswaveformgenerationpsoc5lp

2. Re: Multiple Waveform Generation at the Same Time
user_302397898 Mar 18, 2016 11:04 AM (in response to linghe.wang)The algorithm for generating an array that outputs a sine wave is simple. You can do it in a spreadsheet with the formula =SIN(2*PI()*A1/61)*0.75 where 61 represents the number of samples in a single wave cycle and 0.75 represents amplitude.
If you do many of these in C I think you will soon see certain problems. First, you cannot have 4096 arrays without running out of memory so you would want to reuse arrays after summing the values of two of them.
Second, let's say your array lengths are 128 samples. For any wavelengths that do not have integer multiples of cycles in 128 will not cross at zero on sample [127] when the output wraps back to sample [0]. Thus you will be getting severe crossover distortion on those frequencies. Below is a snippet of code that pregenerates two sine waves of different frequencies and amplitude and sums the two sine waves. I'm not saying it's a solution; indeed, I think it shows what problems you might have. You might want to look for analog solutions in hardware.#define PI 3.14159265
#define AMPLITUDE1 1
#define AMPLITUDE2 0.5
uint8 i,j;
float sinwave1[128], sinwave2[128];
float sumofwaves[128];
for (i=0; i < 128; i++) sumofwaves[i] = 0.0;
for(i = 0; i < 128; i++)
{
sinwave1[i] = (i*2*PI/128) * AMPLITUDE1; // End point wraps at zero to next waveform
sinwave2[i] = (i*2*PI/61) * AMPLITUDE2 ; // Notice that the ends will NOT cross at zero
sumofwaves[i] += sinwave1[i] + sinwave2[i];
}
ouput (sumofwaves); // Some function that will output your signal 
3. Re: Multiple Waveform Generation at the Same Time
bob.marlowe Mar 18, 2016 11:22 AM (in response to linghe.wang)#define PI 3.14159265
We learnt the trick
#define PI (4*atan(1.0)) // this is a constant evaluated at compiletime with best precision
Bob

4. Re: Multiple Waveform Generation at the Same Time
linghe.wang Mar 25, 2016 12:46 PM (in response to user_302397898)I was trying to generate a single sine wave, but I failed. The result showed me a 4V dc voltage(Possibly because I set the DAC scale: 04.08v) Could you please have a look at my code to see if there is something wrong? I also tried your equation sinewave[i] = (i*2*Pi/800)*Amplitude; (I assumed I need 800 samples per period) , but the results were the same. Maybe I thought was totally wrong? I spent lots time in debugging but couldn't figure it out.
My code:
#include <project.h>
#include <math.h>#define TABLE_SIZE 1000//amount of samples
#define TWO_PI (3.14159 * 2)
#define Amplitude 1
#define MaxFreq 400
#define Freq_1 200
#define SamplePerPeriod 800//Based on the maximum frequencyint main()
{
float Samples [TABLE_SIZE];
float TimeIncrement = (1/MaxFreq)/SamplePerPeriod;
int DigitalResults [TABLE_SIZE];
float t[TABLE_SIZE];//index of each sampling time point
float ADCIncrement = (2*Amplitude)/255;
int i;
VDAC8_1_Start();
for (i = 0; i < TABLE_SIZE; i++)
{
t[i] = i*TimeIncrement;
Samples[i] = Amplitude*sin(TWO_PI*Freq_1*t[i] );
//Samples[i] = ((i*TWO_PI)/SamplePerPeriod)*Amplitude;
DigitalResults[i] = (int)((Samples[i]+Amplitude)/ADCIncrement);
VDAC8_1_SetValue(DigitalResults[i]);
CyDelayUs(1); //the time interval is 3.13us
}
// }
return 0;
}Many thanks!!
Regards,
Lingo

5. Re: Multiple Waveform Generation at the Same Time
user_78878863 Mar 28, 2016 2:15 PM (in response to linghe.wang)You could try to create the tables with the data in a first loop, and then use a second loop to send them to the DAC. That way you can put as breakpoint after the first loop (or send it via UART to the PC) and check that the values in there are indeed correct.