From: kk KKsingh on
"Wayne King" <wmkingty(a)gmail.com> wrote in message <htmsa9$7kk$1(a)fred.mathworks.com>...
> "kk KKsingh" <akikumar1983(a)gmail.com> wrote in message <htmqt9$8u9$1(a)fred.mathworks.com>...
> > "kk KKsingh" <akikumar1983(a)gmail.com> wrote in message <htjsok$rl6$1(a)fred.mathworks.com>...
> > > How can i calculate the Energy of the signal in Matlab
> > >
> > > When i try to calculate for positive half i am getting Energy at right frequency ! But some thing is wrong when i do it for whole signal i am not getting at -30 and +30...
> > > N=1024;
> > > dt=.001;
> > > t=0:dt*dt*(N-1);
> > > y=sin(2*pi*30*t);
> > > e=abs(fft(y))/(N);
> > > energy=e.^2;
> > > k=1/dt/N*[-N/2:N/2-1]
> > > plot(k,e)
> > >
> > > I am not able to get where i am doing wrong ! If i do it for positive half using energy=e.^2(1:N/2)..I am getting at Energy30 Hz..but if i use for full then i am not getting at -30 and +30 but getting some where near 470..
> > >
> > > Thanks for your help
> > >
> > > kk
> >
> >
> > Is there any difference between the energy if i calculate it in time domain and in frequency domain !
> >
> > Suppose my signal is x=[1 2 3 4 5 6]
> >
> > So its energy will be x^2...
> >
> > and in Frequency domain
> >
> > its (fft(x))^2
> >
> > As, far as i know there is no change in Energy if signal in both domains!
>
> Hi, it depends on how you define the Fourier transform whether it is a unitary operator or not (energy preserving). The way the DFT is defined in Matlab, it is not energy preserving:
>
> x = [1 2 3 4 5 6];
> xDFT = fft(x);
>
> Note that
> norm(x,2)
> is not equal to
> norm(xDFT,2)
>
> But that
> norm(xDFT,2)/sqrt(length(x))
>
> equals the norm of the time domain signal.
>
> Wayne

Thanks wayne ! Didnt get the logic of sqrt (length(x))...though its working
From: Greg Heath on
% Newsgroups: comp.soft-sys.matlab
% From: "kk KKsingh" <akikumar1...(a)gmail.com>
% Date: Wed, 26 May 2010 19:28:20 +0000 (UTC)
% Local: Wed, May 26 2010 3:28 pm
% Subject: Energy of signal
%
% How can i calculate the Energy of the signal in Matlab
%
% When i try to calculate for positive half i am getting
% Energy at right frequency ! But some thing is wrong
% when i do it for whole signal i am not getting at -30
% and +30...
% N=1024;
% dt=.001;
% t=0:dt*dt*(N-1);

INCORRECT

% y=sin(2*pi*30*t);
% e=abs(fft(y))/(N);
% energy=e.^2;

INCORRECT

% k=1/dt/N*[-N/2:N/2-1]
% plot(k,e)

NOT a plot of energy or power

% I am not able to get where i am doing wrong !
% If i do it for positive half using energy=e.^2(1:N/2)..
% I am getting at Energy30 Hz..but if i use for full then
% i am not getting at -30 and +30 but getting some where
% near 470..


close all, clear all, clc, k=0

N = 1024 % 1024
dt = 0.001 % 1e-3
Fs = 1/dt % 1000 fft period
T = N*dt % 1.024 ifft period

t = 0:dt:T-dt;

f0 = 30 % 30
T0 = 1/f0 % 0.033333 signal period
Nc = T/T0 % 30.72 noninteger number of cycles
y = sin(2*pi*f0*t);

% Power and Energy in the Time Domain

p = y.^2 ; % Instantaneous Power
P = sum(p) % 511.04 Total Power
Pav = mean(p) % 0.49906 Average Power
e = dt*p; % Instantaneous Energy
Et = sum(e) % 0.51104 Total Energy
Etav = mean(e) % 0.00049906 Average Energy

http://groups.google.com/group/comp.soft-sys.matlab/
msg/009339c581e63467?hl=en

% Power and Energy in the Freqency Domain

df = Fs/N % 0.97656 = 1/T
f = 0:df:Fs-df; % Unipolar freq interval
fb = f - df*ceil((N-1)/2); % Bipolar freq interval
Y = fft(y); % Unipolar freq spectrum
Yb = fftshift(Y); % Bipolar freq spectrum

% Note Y and Yb are interchangeable in most of the following

absYb = abs(Yb);
PSDb = absYb.^2/N; % Power Spectral Density

figure
plot(fb,PSDb)
axis([-Fs/2 Fs/2 0 1.1*max(PSDb)])
title('Power Spectrum')

% Check Parseval's Theorem (See Wikipedia)

check = sum(p)-sum(PSDb) % -4.5475e-013

You can determine the appropriate expressions
for the frequency domain calculations of
P, Pav, Ef and Efav

http://groups.google.com/group/comp.soft-sys.matlab/
msg/2222327db2ea7f51


Hope this helps.

Greg
From: Wayne King on
"kk KKsingh" <akikumar1983(a)gmail.com> wrote in message <htnm75$4fj$1(a)fred.mathworks.com>...
> "Wayne King" <wmkingty(a)gmail.com> wrote in message <htmsa9$7kk$1(a)fred.mathworks.com>...
> > "kk KKsingh" <akikumar1983(a)gmail.com> wrote in message <htmqt9$8u9$1(a)fred.mathworks.com>...
> > > "kk KKsingh" <akikumar1983(a)gmail.com> wrote in message <htjsok$rl6$1(a)fred.mathworks.com>...
> > > > How can i calculate the Energy of the signal in Matlab
> > > >
> > > > When i try to calculate for positive half i am getting Energy at right frequency ! But some thing is wrong when i do it for whole signal i am not getting at -30 and +30...
> > > > N=1024;
> > > > dt=.001;
> > > > t=0:dt*dt*(N-1);
> > > > y=sin(2*pi*30*t);
> > > > e=abs(fft(y))/(N);
> > > > energy=e.^2;
> > > > k=1/dt/N*[-N/2:N/2-1]
> > > > plot(k,e)
> > > >
> > > > I am not able to get where i am doing wrong ! If i do it for positive half using energy=e.^2(1:N/2)..I am getting at Energy30 Hz..but if i use for full then i am not getting at -30 and +30 but getting some where near 470..
> > > >
> > > > Thanks for your help
> > > >
> > > > kk
> > >
> > >
> > > Is there any difference between the energy if i calculate it in time domain and in frequency domain !
> > >
> > > Suppose my signal is x=[1 2 3 4 5 6]
> > >
> > > So its energy will be x^2...
> > >
> > > and in Frequency domain
> > >
> > > its (fft(x))^2
> > >
> > > As, far as i know there is no change in Energy if signal in both domains!
> >
> > Hi, it depends on how you define the Fourier transform whether it is a unitary operator or not (energy preserving). The way the DFT is defined in Matlab, it is not energy preserving:
> >
> > x = [1 2 3 4 5 6];
> > xDFT = fft(x);
> >
> > Note that
> > norm(x,2)
> > is not equal to
> > norm(xDFT,2)
> >
> > But that
> > norm(xDFT,2)/sqrt(length(x))
> >
> > equals the norm of the time domain signal.
> >
> > Wayne
>
> Thanks wayne ! Didnt get the logic of sqrt (length(x))...though its working

Hi, It's because the basis vectors for the DFT e^{j 2\pi k n/N} have unit (l2) norm when they are divided by \sqrt{N}, so that e^{j 2\pi k n/N}/\sqrt{N} k=0,1, ... N-1 form an orthonormal set of N-periodic sequences.

If you wanted the DFT to be a unitary operator, you would have a 1/sqrt{N} factor both in the Fourier transform from the time domain to the frequency domain AND in the "inverse" Fourier transform from the frequency domain to the time domain. However, many people define the Fourier transform by incorporating the product of those factors 1/N into either the transform from time to frequency or from frequency to time. That is the way that MATLAB defines it. In MATLAB, the 1/N factor is incorporated in the "inverse" transform from frequency to time, therefore the l2 norm squared in time is equal to 1/N times the l2 norm squared in frequency.

Wayne

Wayne