Computing Fourier Coefficients and displaying data tia sal22

Discussion in 'MATLAB' started by Rick T, Jan 15, 2010.

  1. Rick T

    Rick T Guest

    Greetings All

    I found some great matlab code that creates Fourier Coefficients and
    I’m trying to display/export the coefficients into the format y=ao
    +an*cos(n*2*pi*x)+bn*sin(n*2*pi*x). I’ve plotted y vs ynew using
    matlab and matrices which matches up great. But I'm having problems
    calculating the matrices and displaying/getting them into the correct
    format y=ao+an*cos(n*2*pi*x)+bn*sin(n*2*pi*x).

    I test the output in geogebra a nice free program to check the
    periodicity of the signal however the signal looks just like a sine
    wave.

    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,32);
    wt=2*pi*x;
    y=2*sin(wt)+cos(3*wt); %test signal

    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=17; %should be all the columns in C or S

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

    %Write to file/disp
    disp(['y=ao + Amp*(',num2str(An(1)/2),'+']) %then append to file
    nn=0;
    for ii=2:length(An),
    nn=nn+1;
    disp([num2str(An(ii)),'*cos(',num2str(nn),'*freq*x+offset)',num2str
    (Bn(ii)),'*sin(',num2str(nn),'*freq*x+offset) +']);
    end;
    disp([')'])

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

    some output below:

    y=ao + Amp*(0.03125+-0.12324*cos(1*freq*x+offset)1.962*sin(1*freq*x
    +offset) +
    0.12171*cos(2*freq*x+offset)-0.065728*sin(2*freq*x+offset)…left off
    rest of output to keep it readable

    My ultimate goal is to be able to make the different signals periodic
    so I can adjust the different characteristics of the signal
    (frequency, amplitude, offset, etc) by adjusting a few variables
    basically in the y=ao+an*cos(n*2*pi*x)+bn*sin(n*2*pi*x) format.

    Tia sal22
     
    Rick T, Jan 15, 2010
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.