DSPRelated.com
Forums

Generating square waveform of 40kHz (problem)

Started by angr...@hotmail.co.uk November 26, 2009
Hi all
I am trying to generate a square wave on TIDSKC6713 (CCS) by modifying a code from Chassaing book (chapter 2). I want to generate the square wave with variable frequency fo upto 40kHz (by using 96kHz sampling frequency). I am using a buffer of 128 values with half for positive and half for negative amplitudes. I have used two slider GEL; one for variable gain and other for variable frequency. With gain I could get from 50mVpp to 2.8Vpp which seems OK to me but with frequency from 750Hz to 8kHz the output is square (with some ripples) but above that the output is not square any more and starts modifying itself to the shape of sinusoidal waveform.
I have copied the modified code. It would be much appreciated if some one could help me in this regard please.....
Many Thanks
#include "dsk6713_aic23.h" //codec-DSK interface support
Uint32 fs=DSK6713_AIC23_FREQ_96KHZ; //set sampling rate
#define table_size (int)0x80 //size of table8
short data_table[table_size]; //data table array
int i,k;
int amplitude = 0x7fff;
short gain = 1;
int frequency = 1;
interrupt void c_int11() //interrupt service routine
{

output_sample(data_table[i] / gain); //output value each Ts

i += frequency;
i = i % table_size;
//reinitialize counter
return; //return from interrupt
}

main()
{
for(i=0; i data_table[i] = amplitude; //with max value (2^15)-1
for(i=table_size/2; i data_table[i] = -(amplitude + 1); //with -(2^15)

i = 0; //reinit counter
comm_intr(); //init DSK, codec, McBSP

while (1)

}

_____________________________________
Angry Dude-

> I am trying to generate a square wave on TIDSKC6713 (CCS) by
> modifying a code from Chassaing book (chapter 2). I want
> to generate the square wave with variable frequency fo upto
> 40kHz (by using 96kHz sampling frequency). I am using a
> buffer of 128 values with half for positive and half for
> negative amplitudes. I have used two slider GEL; one for
> variable gain and other for variable frequency. With gain I
> could get from 50mVpp to 2.8Vpp which seems OK to me but
> with frequency from 750Hz to 8kHz the output is square (with
> some ripples) but above that the output is not square any
> more and starts modifying itself to the shape of sinusoidal
> waveform. I have copied the modified code. It would be much
> appreciated if some one could help me in this regard please.....

If your GEL slider translates directly to your C code variable "frequency", then for fu0 the formula below causes a
sequence 1,110,92,74,56,38,20,14,124,106,88,70,8... i.e. high for 4 samples, low for 4 samples, etc. At Fs kHz,
that's a square wave with frequency 6 kHz, not 750 Hz.

With f00 the sequence flips every other sample, which explains why you no longer get "squareness" and instead see a
sine wave that looks like 48 kHz.

I submit that you're seeing (I assume on a dig scope) what the numbers say you should be seeing, but a) your Fs is too
high, b) your GEL frequency slider needs a multiplier < 1, or c) you haven't coded it correctly.

-Jeff

> #include "dsk6713_aic23.h" //codec-DSK interface support
> Uint32 fs=DSK6713_AIC23_FREQ_96KHZ; //set sampling rate
> #define table_size (int)0x80 //size of table8
> short data_table[table_size]; //data table array
> int i,k;
> int amplitude = 0x7fff;
> short gain = 1;
> int frequency = 1;
> interrupt void c_int11() //interrupt service routine
> {
>
> output_sample(data_table[i] / gain); //output value each Ts
>
> i += frequency;
> i = i % table_size;
> //reinitialize counter
> return; //return from interrupt
> }
>
> main()
> {
> for(i=0; i > data_table[i] = amplitude; //with max value (2^15)-1
> for(i=table_size/2; i > data_table[i] = -(amplitude + 1); //with -(2^15)
>
> i = 0; //reinit counter
> comm_intr(); //init DSK, codec, McBSP
>
> while (1)
>
> }

_____________________________________
Naeem-

> thanks for your help. I'm bit confused about slider coding. Does
> it mean that by using the existing code but changing
> the slider coding, I would be able to generate 40kHz square
> waveform!?!?

Yes you can conceivably get a 40 kHz output with Fs = 96 kHz, but on the scope it won't look like a square wave...
more like a distorted sine wave. I would say to see a decent-looking square wave on the scope your max would be
around 5 to 10 kHz. If you think about the practical limit of you can do at Fs/2 (sine wave with every other point +A
and -A) then you can apply theory from your DSP classes and consider what's possible as your rate decreases to Fs/4
and so on.

I'm not sure what your GEL slider does... my only comment would be to take slider settings and figure out resulting
sequences of your 'frequency' var (and table indexes) and make sure you end up with correct number of +A and -A
samples. Once you derive the correct adjustment factor it should work for all cases.

-Jeff

PS. Please always post to the group so other DSP students can benefit from the discussion.

> At the moment
>
> When frequecy slider = 1 then output is 750Hz as 96kHz/128 = 750Hz
>
> When frequency slider = 2 then ouput is 1500Hz as 96kHz/64 = 1500Hz
>
> .
>
> .
>
> .
>
> When frequency slider = 8 then ouput is 6000Hz as 96kHz/16 = 6kHz
>
> When frequency slider = 16 then ouput should be 12000Hz as 96kHz/8 = 12kHz
>
> And so on... but if i'm doing some thing wrong in this logic!?!?!
>
> It would be much appreciated if you could correct me in this wrong logic please
>
> Many Thanks
>
> Naeem

_____________________________________
Hi Jeff
Many Thanks for your help
So, what I understood is that I would be able to generate and observe on oscilloscope 40kHz sine wave but not 40kHz sqaure waveform due to limitations of the dsk board. Max I can get would be up to 10kHz... If I am right.... & there is no other way that I can send a value (say 32767 for +ve) via McBSP to AIC, wait for x seconds then send -32768 for -ve and again wait for x seconds to generate a sqaure waveform for higher frequency ranges (say 10kHz to 40kHz...) (adjusting x for 50% duty cycle)

Many Thanks

Hi all
>I am trying to generate a square wave on TIDSKC6713 (CCS) by modifying a code from Chassaing book (chapter 2). I want to generate the square wave with variable frequency fo upto 40kHz (by using 96kHz sampling frequency). I am using a buffer of 128 values with half for positive and half for negative amplitudes. I have used two slider GEL; one for variable gain and other for variable frequency. With gain I could get from 50mVpp to 2.8Vpp which seems OK to me but with frequency from 750Hz to 8kHz the output is square (with some ripples) but above that the output is not square any more and starts modifying itself to the shape of sinusoidal waveform.
>I have copied the modified code. It would be much appreciated if some one could help me in this regard please.....
>Many Thanks
>#include "dsk6713_aic23.h" //codec-DSK interface support
>Uint32 fs=DSK6713_AIC23_FREQ_96KHZ; //set sampling rate
>#define table_size (int)0x80 //size of table8
>short data_table[table_size]; //data table array
>int i,k;
>int amplitude = 0x7fff;
>short gain = 1;
>int frequency = 1;
>interrupt void c_int11() //interrupt service routine
> {
>
> output_sample(data_table[i] / gain); //output value each Ts
>
> i += frequency;
> i = i % table_size;
> //reinitialize counter
> return; //return from interrupt
> }
>
>main()
>{
> for(i=0; i

_____________________________________
Naeem-

> Many Thanks for your help
> So, what I understood is that I would be able to generate and
> observe on oscilloscope 40kHz sine wave but not 40kHz
> sqaure waveform...

Yes.

> ...due to limitations of the dsk board.

The correct perspective is that the sampling rate is the limitation, not the DSK board. At Fs kHz, the max
possible waveform frequency you could possibly obtain is 48 kHz, but even that is a theoretical max -- maybe you would
actually see something a few kHz less.

> Max I can get would be up to 10kHz... If I am right.... & there
> is no other way that I can send a value (say 32767 for +ve) via
> McBSP to AIC, wait for x seconds then send -32768 for
> -ve and again wait for x seconds to generate a sqaure waveform
> for higher frequency ranges (say 10kHz to 40kHz...)

No that won't work. I suggest that you spend time to study Nyquist theorem and other basic DSP theory.

-Jeff

> Hi all
>>I am trying to generate a square wave on TIDSKC6713 (CCS) by modifying a code from Chassaing book (chapter 2). I want
>> to generate the square wave with variable frequency fo upto 40kHz (by using 96kHz sampling frequency). I am using a
>> buffer of 128 values with half for positive and half for negative amplitudes. I have used two slider GEL; one for
>> variable gain and other for variable frequency. With gain I could get from 50mVpp to 2.8Vpp which seems OK to me but
>> with frequency from 750Hz to 8kHz the output is square (with some ripples) but above that the output is not square
>> any more and starts modifying itself to the shape of sinusoidal waveform.
>>I have copied the modified code. It would be much appreciated if some one could help me in this regard please.....
>>Many Thanks
>>#include "dsk6713_aic23.h" //codec-DSK interface support
>>Uint32 fs=DSK6713_AIC23_FREQ_96KHZ; //set sampling rate
>>#define table_size (int)0x80 //size of table8
>>short data_table[table_size]; //data table array
>>int i,k;
>>int amplitude = 0x7fff;
>>short gain = 1;
>>int frequency = 1;
>>interrupt void c_int11() //interrupt service routine
>> {
>>
>> output_sample(data_table[i] / gain); //output value each Ts
>>
>> i += frequency;
>> i = i % table_size;
>> //reinitialize counter
>> return; //return from interrupt
>> }
>>
>>main()
>>{
>> for(i=0; i

_____________________________________