The power spectrum of a sine wave (C language)

Click For Summary

Discussion Overview

The discussion revolves around deriving the power spectrum of a sinusoidal signal using the discrete Fourier transform (DFT) in C programming. Participants explore the conditions under which the power spectrum can be represented as a delta function and the implications of frequency alignment with Fourier frequencies.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant shares their C code for calculating the power spectrum of a sinusoid and expresses confusion about why the power spectrum does not appear as a delta function.
  • Another participant suggests that the sums in the code yield non-zero values for frequencies other than the sinusoidal frequency, prompting a question about the conditions for this occurrence.
  • Some participants propose that the frequency of the sinusoid must match one of the Fourier frequencies for the power to concentrate in a single bin of the DFT.
  • There is a discussion about the integration interval and its relation to the conditions under which certain integrals yield non-zero results.
  • One participant mentions the need to consider spectral leakage and windowing effects when summing over cycles of the signal.
  • Another participant expresses a desire to understand what changes are necessary to achieve a delta function representation in the power spectrum.

Areas of Agreement / Disagreement

Participants generally agree that the alignment of the sinusoidal frequency with the Fourier frequencies is crucial, but the exact conditions and implications remain contested and unresolved.

Contextual Notes

Participants reference specific mathematical conditions and integrals without resolving the underlying assumptions or dependencies on definitions related to the Fourier transform and power spectrum.

Who May Find This Useful

This discussion may be useful for individuals interested in signal processing, Fourier analysis, and programming in C, particularly those exploring the relationship between sinusoidal signals and their power spectra.

arcTomato
Messages
104
Reaction score
27
Homework Statement
Derive the power spectrum of sinusoid
Relevant Equations
descrete fourier transform
Hi

I would like to Derive the power spectrum of sinusoid.I tried like this. But It doesn't work.

<Moderator: CODE tags added>
C:
#include <stdio.h>
#include <math.h>

#define pi 3.1415926535    
FILE *in_file, *out_file;
int main()
{
    dft();
}
int dft(int argc, char *argv[])
{ 
    char *filename_in = argv[1];
    char *filename_out = argv[2];
    if(argc != 3){
        printf("使い方: ./a.out <入力ファイル名> <出力ファイル名>\n");
        return 0;
    }
    int j, k, N;
    int max = 100000;
    double f[max], re = 0, I am = 0;
    if((in_file=fopen(filename_in,"r"))==NULL){
        printf("in_file cannot open\n");
        return 0;
    }

    for(N=0; N<max; N++) {
        if(fscanf(in_file,"%lf", &f[N]) == EOF) break;
    }
    fclose(in_file);

    if((out_file=fopen(filename_out,"w"))==NULL){
        printf("outfile cannot open\n");
        return 0;
    }
    //DFT part
    for(j=(-N/2); j<N/2; j++) {
        for(k=0; k<N; k++) {
            re += f[k]*cos(2*pi*j*k/N);
            I am += -f[k]*sin(2*pi*j*k/N);
        }
        fprintf(out_file,"%d, %f\n", j, re*re+im*im);
    }

    fclose(out_file);
    return 0;
}
When sinusoid frequancy is 7 and the sampling time series ##T=0.01s##, this is the power spectrum.

1573374414734.png
The paper I read says that "Only when the frequency of the sinusoid is equal to one of the Fourier frequencies will all power be concentrated in one bin of the discrete Fourier transform.", so I think the power spectrum should be delta function.

How can I derive the power spectrum as delta function?
Thank you.
 
Last edited by a moderator:
Physics news on Phys.org
Apparently your sums add up to non zero values for other frequencies. Under which circumstances can that happen ?
(hint: look at your homework equations :smile: )
 
Thanks @BvU!
I think "the frequency of the sinusoid is equal to one of the Fourier frequencies" is the circumstances.
But I don't understand why,,,,
 
I began to read your code, and desisted when I encountered the following character string that I perceived to be inclusive of non-Roman characters: printf("使い方: ./a.out <入力ファイル名> <出力ファイル名>\n");
 
thanks @sysprog!

What is wrong with that?That part is usage.
 
  • Informative
Likes   Reactions: BvU
arcTomato said:
Thanks @BvU!
I think "the frequency of the sinusoid is equal to one of the Fourier frequencies" is the circumstances.
But I don't understand why,,,,
As an example: Suppose your signal is ##\sin x##, under what circumstances can e.g. $$\int_a^b \sin x \sin 2x\ dx $$come out non-zero. Or, turned around: what are the conditions for $$\int_a^b \sin x \sin 2x\ dx \ = 0 $$
 
Thanks for your kindness , @BvU!
The integration interval is ##2π##?
So What should I change to derive the power spectrum as delta function?
Data number??or Sampling time series??
 
Did you guess the answer to my question ?
Read up on spectral leakage, windowing and such.

For your signal you want to make sure you integrate (sum) over an integer multiple of cycles (both from the signal and the sampling wave 2*π*j*k/N).

(With that I answered my own question :woot: )
 
Thank you @BvU!

I think I got it:>
I will try it!
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K