0
$\begingroup$

I'm reading "Digital Signal Processing", written by Michael Weeks. In section Representing a Digital Signal as a Sum of Sinusoids, there is this Matlab code that I cannot understand. (code given below)

What I don't get is what does the term inside cos argument 1/Xsize mean (in part %Do the sinusoids as discrete points only, I guess it is fundamental frequency, but how can we conclude it). And why is the whole function divided by Xsize.

Same thing with smooth0, smooth1, smooth2... I also don't get this smoothing process.

Thank you for any explanations, or links to helpful stuff.

%sum_of_sins.m
%Show a random signal as a sum of sins
%

%Make our x signal
x=round(rand(1,8)*10);
Xsize=length(x);

%Get FFT of x
X=fft(x);
Xmag=abs(X);
Xphase=angle(X);

%Show the freq-domain info as sum of sinusoids
%Find the IFFT (the hard way) part 1
n=0:Xsize-1;
%Do the sinusoids as discrete points only
s0=Xmag(1)*cos(2*pi*0*n/Xsize+Xphase(1))/Xsize;
s1=Xmag(2)*cos(2*pi*1*n/Xsize+Xphase(2))/Xsize;
s2=Xmag(3)*cos(2*pi*2*n/Xsize+Xphase(3))/Xsize;
s3=Xmag(4)*cos(2*pi*3*n/Xsize+Xphase(4))/Xsize;
s4=Xmag(5)*cos(2*pi*4*n/Xsize+Xphase(5))/Xsize;
s5=Xmag(6)*cos(2*pi*5*n/Xsize+Xphase(6))/Xsize;
s6=Xmag(7)*cos(2*pi*6*n/Xsize+Xphase(7))/Xsize;
s7=Xmag(8)*cos(2*pi*7*n/Xsize+Xphase(8))/Xsize;
%Redo the sinusoids as smooth curves
t=0:0.05:Xsize-1;
smooth0=Xmag(1)*cos(2*pi*0*t/Xsize+Xphase(1))/Xsize;
smooth1=Xmag(2)*cos(2*pi*1*t/Xsize+Xphase(2))/Xsize;
smooth2=Xmag(3)*cos(2*pi*2*t/Xsize+Xphase(3))/Xsize;
smooth3=Xmag(4)*cos(2*pi*3*t/Xsize+Xphase(4))/Xsize;
smooth4=Xmag(5)*cos(2*pi*4*t/Xsize+Xphase(5))/Xsize;
smooth5=Xmag(6)*cos(2*pi*5*t/Xsize+Xphase(6))/Xsize;
smooth6=Xmag(7)*cos(2*pi*6*t/Xsize+Xphase(7))/Xsize;
smooth7=Xmag(8)*cos(2*pi*7*t/Xsize+Xphase(8))/Xsize;
%Find the IFFT (the hard way) part 2
my_sum_of_sins=s0+s1+s2+s3+s4+s5+s6+s7;
smooth_sum=smooth0+smooth1+smooth2+smooth3+smooth4+smooth5+smooth6+smooth7;

%Show both discrete points and smooth curves together
xaxis1=(0:length(smooth0)-1)/20; %for 8 points
xaxis2=(0:length(s0)-1);

figure(1);
subplot(4,1,1); plot(xaxis1, smooth0, 'g', xaxis2, s0,'r*');
subplot(4,1,2); plot(xaxis1, smooth1, 'g', xaxis2, s1,'r*');
subplot(4,1,3); plot(xaxis1, smooth2, 'g', xaxis2, s2,'r*');
subplot(4,1,4); plot(xaxis1, smooth3, 'g', xaxis2, s3,'r*');

figure(2);
subplot(4,1,1); plot(xaxis1, smooth4, 'g', xaxis2, s4, 'r*');
subplot(4,1,2); plot(xaxis1, smooth5, 'g', xaxis2, s5, 'r*');
subplot(4,1,3); plot(xaxis1, smooth6, 'g', xaxis2, s6, 'r*');
subplot(4,1,4); plot(xaxis1, smooth7, 'g', xaxis2, s7, 'r*');
$\endgroup$

1 Answer 1

1
$\begingroup$

Effectively Xsize is allows the sampling frequency to be set.

If you just used 2*pi*0*n+Xphase(1) as the argument of $\cos(\cdot)$ or $\sin(\cdot)$ then you'd just get $\cos({\tt Xphase(1)})$ or $\sin({\tt Xphase(1)})$ for all terms... because then the 2*pi*0*n terms would be an integer multiple of 2*pi.


EDIT With respect to the Xsize terms at the end:

s0=Xmag(1)*cos(2*pi*0*n/Xsize+Xphase(1))/Xsize;

Check out the definition of the inverse DFT, particularly equation 2: $$ x_n = \frac{1}{N} \sum_{k=0}^{N-1} X_k e^{i2\pi k n / N} $$ for your code, N = XSize.

$\endgroup$
4
  • $\begingroup$ Thank you for reply. So does that mean that he supposed the signal to be 1 sec long, and that those sinusoids s0,s1,s2... in continuous form has frequencies of 0,1,2,3,4... Hz. But why does he divide all by /Xsize. one with asterisks --> smooth0=Xmag(1)*cos(2*pi*0*t/Xsize+Xphase(1))/**Xsize**; $\endgroup$
    – balboa
    Nov 22, 2013 at 17:16
  • 1
    $\begingroup$ @balboa: See my update. $\endgroup$
    – Peter K.
    Nov 22, 2013 at 17:24
  • $\begingroup$ And is it true what I have written about frequencies in continuous/analog form of signal and that signal is 1 sec long (in my previous comment)? $\endgroup$
    – balboa
    Nov 22, 2013 at 17:30
  • $\begingroup$ Effectively, yes: your time index is effectively [0:XSize-1]/XSize... i.e. 0 to [1-1/XSize] seconds. $\endgroup$
    – Peter K.
    Nov 22, 2013 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.