# Fourier equation and getting out of memory error tia sal22

Discussion in 'MATLAB' started by Rick T, Sep 3, 2010.

1. ### Rick TGuest

Fourier equation and out of memory error tia sal22
Sorry is this got posted twice my posts seem not to be showing up in
some groups...usenet gremlins again

Greetings All

I can get this matalb script to work and plot when I set
x=linspace(0,1,100);
But when I set x=linspace(0,1,44100); I get a mtimes out of memory
which I narrowed down to the large array size in the C and S variables

Can someone recommend a way for this code to work without running
into memory errors? I tried going the fft route but the signal would
never match up.

The plot looks great I just would like to:
1) not have a memeory error issue if I set x=linspace(0,1,44100); or
another large number in there.
2) Would like to compare the the y array to the ynew array and get a
percentage of error is this possible?

See working code below:

clear
clc
t1 = tic; %start timer to see how long it takes to process

%
% DFT in a compact implementation using matrix math faster
%
% Enter Data in y
x=linspace(0,1,100);
wt=2*pi*x;
y=sin(wt)+cos(2*wt);

N=length(y);
% Compute the matrices of trigonometric functions
p=1:N/2+1;
n=1:N;
C=cos(2*pi*n'*(p-1)/N);
S=sin(2*pi*n'*(p-1)/N);
% Compute Fourier Coefficients
An=2/N*y*C;
Bn=2/N*y*S;

An(N/2+1)=An(N/2+1)/2;
% Reconstruct Signal - pmax is number of frequencies used in
increasing order

%pmax=5; %should be all the columns in C or S or less to decrease
equation
pmax=(size(An,2));

ynew=An(1)/2+C,2max)*An(2max)'+S,2max)*Bn(2max)';

% % Plot Data
plot(x,y,'r')
% Plot reconstruction over data
hold on
plot(x,ynew,'g*')
hold off
toc(t1) %stops timer and tells how long it took to run

Rick T, Sep 3, 2010

2. ### Greg HeathGuest

'1. Close existing plots'

close all
'2: ERROR: t1 = tic is invalid syntax'

tic
'3: Nonoptimal choice of x and wt. Will get spectral'
' leakage into other frequency bins'

'4: Use y = 1 + sin(wt) + cos(2*wt) to make sure you'
' handle DC correctly'
'5. ERROR: use (n-1)'
'6. Switch (n-1) and (p-1) to premultiply y'
'7. n is the time index; use Ap and Bp'
'8. Similary for An(1) '
'9. Display spectral leakage due to choice of time window'

display([An' Bn'])
'10: Wrapped comment error ... use shorter lines;'
' e.g., 55 characters is good'
'11: Wrapped comment error'
'12: Choosing pmax = 5 will INCREASE the size of the equation.'
' However, it will eliminate most of the spectral leakage '
' in other bins caused by the nonoptimal choice of time window.'
'13. ynew is a column vector'

dy = abs(y-ynew'); % absolute error
MAE = mean(dy) % 7.2292e-015 mean-absolute-error
MXE = max(dy) % 3.4195e-014 maximum-absolute-error
RMSE = sqrt(mean(dy.^2)) % 9.5049e-015 root-mean-sqare-error

'14. For pmax = N/2+1:'

[Amax maxpA] = max(An) % [ 1.0032 3 ] % error due to leakage
[Bmax maxpB] = max(Bn) % [ 0.99515 2 ] % error due to leakage

dy1 = abs( C,4:end)*An(4:end)'+S,4:end)*Bn(4:end)' );

MAE = mean(dy1) % 0.012452 mean-absolute-error
MXE = max(dy1) % 0.069252 maximum-absolute-error
RMSE = sqrt(mean(dy1.^2)) % 0.016299 root-mean-sqare-error

'15: Initialize figure'

figure(1)
'16. ERROR: toc(t1) is invalid syntax'

toc

'17. It would be very helpful if you made sure that your'
'posted code will run when cut and pasted into an m-file.'

'Hope this helps.'

'Greg'

Greg Heath, Sep 3, 2010

3. ### Rick TGuest

the spectral leakage. Along with
generating the correct combination
of periodic function and time window.

Aloha and thanks again for you help

Rick T, Sep 4, 2010