Pregunta

I'm working on an assignment to perform a 200 point DFT at a sampling frequency of 20kHz on a square wave of frequency 500Hz whose amplitude alternates between 0 and 20.

I'm using C++ and I have figured how to code the DFT equation, my problem is I'm having trouble representing the square wave in code using a for loop.

What I'm really still confused about is how many cycles of this square wave will be in my 200 point sample.

Thanks

¿Fue útil?

Solución

One cycle of your square wave will take 1/500 seconds. Each sample will be 1/20000 seconds. A simple division should tell you how many samples each square wave will be.

Another division will tell you how many of those waves will fit in your 200 point window.

Otros consejos

The period of the square wave is 20000/500=40 points, so you'll have exactly 5 periods of the square wave in your 200-point sample (200/40=5).

If your sampling frequency is 20,000 Hz and you have a square wave of frequency 500 Hz, this basically means that you will have 500 cycles of your wave per second, which means you will have 500 cycles in every 20,000 samples. This means that each wave cycle requires 40 samples (or points), so if you have 200 points that means you should have 5 square wave cycles within your DFT.

You can make sure you do your calculation right by including the units in your calculation. So the period has the dimension time, Hertz has the dimension of 1.0/time and samples is dimensionless. Programatically, you can do this with boost.units. It will check your units at compile time and give you an error if you make a mistake.

It will also stop your user from entering the wrong units into your code. For example, by entering 20 instead of 20000 for the frequency (thinking you were measuring in kHz)

Your interface will then be something like

using namespace boost::units;
set_period(quantity<si::time> period);

The user will have to enter the time in seconds,

set_period(5*si::seconds)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top