The power spectrum of a sine wave (C language)

In summary, according to the author, the power spectrum of a sinusoid should be a delta function if the frequency of the sinusoid is equal to one of the Fourier frequencies.
  • #1
arcTomato
105
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
  • #2
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: )
 
  • #3
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,,,,
 
  • #4
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");
 
  • #5
thanks @sysprog!

What is wrong with that?That part is usage.
 
  • Informative
Likes BvU
  • #6
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 $$
 
  • #7
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??
 
  • #8
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: )
 
  • #9
Thank you @BvU!

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

FAQ: The power spectrum of a sine wave (C language)

1. What is the power spectrum of a sine wave?

The power spectrum of a sine wave is a graphical representation of the power or energy distribution across different frequencies within the signal. It shows the amplitude of each frequency component present in the signal and is often used in signal processing and analysis.

2. How is the power spectrum of a sine wave calculated?

The power spectrum of a sine wave can be calculated by taking the Fourier transform of the signal, squaring the magnitude of the complex result, and plotting it against frequency. This process is known as power spectral density estimation and is commonly implemented using algorithms such as Fast Fourier Transform (FFT).

3. What is the significance of the power spectrum of a sine wave?

The power spectrum of a sine wave provides important information about the frequency content and strength of a signal. It can be used to identify and analyze different components within a signal, to detect any noise or interference, and to compare signals with different frequencies or amplitudes.

4. How is the power spectrum of a sine wave used in C language?

In C language, the power spectrum of a sine wave can be calculated and plotted using various libraries and functions such as FFTW (Fastest Fourier Transform in the West) or the DSP library. These libraries provide efficient and optimized algorithms for computing the power spectral density of a given signal.

5. Can the power spectrum of a sine wave be used for filtering or denoising?

Yes, the power spectrum of a sine wave can be used for filtering or denoising a signal. By analyzing the power spectrum, specific frequency components can be identified and removed from the signal, resulting in noise reduction or signal enhancement. This is commonly used in audio and image processing applications.

Similar threads

Replies
2
Views
1K
Replies
3
Views
1K
Replies
2
Views
4K
Replies
10
Views
2K
Replies
1
Views
4K
Replies
4
Views
2K
Replies
7
Views
2K
Replies
4
Views
2K
Replies
3
Views
1K
Back
Top