Solving Sea Surface Height with FFT in MATLAB

In summary, the conversation discusses using the 'fft' function in MATLAB to solve a problem involving sea surface height evolution. The three parameters, A, k, and \omega, are used to calculate \eta(x,t) and the deep water dispersion relationship is applied. The A_n constants are found by taking a fft of \eta(0,t) and the frequency vector is constructed based on the length of elements in \eta and the sampling frequency. The code for this is shown, but there are concerns about efficiency and removing unwanted solutions.
  • #1
nickthequick
53
0
Hi,

I have the following problem that I would like to solve by using the 'fft' function in matlab.

Some background on the problem: The evolution of sea surface height is given, to first order, by

[tex]
\eta_o(x,t)=A\cos\left(kx-\omega t\right)
[/tex]

From this, we see that there are three parameters, A, k, \omega, that dictate \eta(x,t). According to linear theory, in general \eta will be a super position of these waves and will be given by

[tex]
\eta(x,t) = \sum_n A_n \cos\left(k_nx-\omega_nt \right)
[/tex]
Each of these waves obey the deep water dispersion relationship

[tex]
\omega_n^2=gk_n
[/tex]

Finally, to find the A_n, we note that we know \eta(0,t). The A_n are constants, therefore we can find them by inverting the relation

[tex]
\eta(0,t)= \sum_n A_n \cos\left(\omega_nt \right)
[/tex]

which is done in MATLAB by taking a fft of \eta(0,t). The last pieces of information I need are the \omega_n s. I'm not sure what sets the frequencies of the system and this is what is giving me trouble. Of course there might be a significantly easier way to do all of this.

Any help would be appreciated,Nick
 
Physics news on Phys.org
  • #2
not quite sure where you're stuck.

from the top, you'd define the function numerically by first defining the range. I.e., you have to define all the variables and constants in your equation. A variable is defined such that

Code:
t = 0:.1:10

would make a vector called "t" that has values form 0 to 10 in steps of .1, the units will be seconds if you choose the units of [tex]\omega[\tex] to be radians per second.

now you can define w, x, k as constants. If any of those are variables as well, this becomes a higher dimension problem.

now you can define eta:

Code:
eta = sin(k*x-w*t)

Sometimes, to get the proper vector operation on an expression, you have to put a period (.) in front of the operator, like "w.*t" instead of "w*t". So keep that in mind if you get errors.

Finally, the fft function is called:

Code:
fft_eta = fft(eta)

This is the vector of amplitudes for your fft. To construct the frequency vector, "f", you have to base it on the length of elements in eta and your sampling frequency (1/dt)

Code:
f = 0:fs/length(eta):fs/2 - fs/length(eta)

fs/2 is referred to as the nyquist frequency.
 
  • #3
Pythagorean,

Thanks for the help! I guess my main issue was the seemingly opaque way that MATLAB defined the independent variable frequency for the Fourier transform. I found a nice tutorial http://blinkdagger.com/matlab/matlab-fourier-transform-with-freqz/".My code is, however, very inefficient. Does anyone see a better way to do this?

g=9.81; %gravity
Fs=50; %sampling frequency

t=(0:length(WG_all_resync.WG_mean(1,:))-1)/50; %time length all data was sampled
y1=packet49; %this is the output of my wave maker and is the signal I'm processing
y=[y1;zeros(7731,1)]; %pads the output to be the (temporal) length that the data is observed
[Y,f]=positiveFFT(y,Fs); %this function calculates the positive part of the fft as well as the associate frequencies
k=(2*pi*f).^2/g; %defining wavenumbers based on deep water disp relation
x=linspace(0,169,length(k)); %is this the proper way to define the x-ordinate?

eta=zeros(length(f),length(k));
for i =1:length(f)
for j=1:length(k)
eta(i,j)=real(sum(Y'.*cos(k*x(j)-2*pi*f*t(i))));
end
end

Also, I think a lot of the subtleties associated with defining the independent variables are lost on me. For instance, I'm not sure if I'm defining x and t properly. Are they dependent on the way I define f and k?

Any help would be appreciated. Thanks!Nick
 
Last edited by a moderator:
  • #4
I have a further question regarding these results.

My results are shown below.

There are two things that are bothering me. For one, MATLAB has indexed my time axis backwards, so in order to plot my time values appropriately i have to make them negative. Is there an easy way to fix this?

The second thing is that because this solution is periodic, I'm seeing artificial "noise" from the other solutions. Is there an easy way to remove these unwanted solutions so that I just display, in effect, the primary branch?

Thanks,

Nick
 

Attachments

  • exampleplot.jpg
    exampleplot.jpg
    59.2 KB · Views: 532
  • #5
g=9.81; %gravity
Fs=50; %sampling frequency

you can do this on one line:

g=9.81;Fs=50; %gravity and sampling rate

t=(0:length(WG_all_resync.WG_mean(1,:))-1)/50; %time length all data was sampled
y1=packet49; %this is the output of my wave maker and is the signal I'm processing
y=[y1;zeros(7731,1)]; %pads the output to be the (temporal) length that the data is observed
[Y,f]=positiveFFT(y,Fs); %this function calculates the positive part of the fft as well as the associate frequencies...

...

x=linspace(0,169,length(k)); %is this the proper way to define the x-ordinate?

I wouldn't hard code the 50, 7731, 169 or the 1 that designates which row of WG_mean to pick from; I'd call theme by variables and define the variables at the beginning of the code. you might have already defined the 50 as Fs (sampling frequency).

for x though, I would use a mathematical definition as I did for the frequency vector instead of relying on matlab's linspace function.

nickthequick said:
eta=zeros(length(f),length(k));
for i =1:length(f)
for j=1:length(k)
eta(i,j)=real(sum(Y'.*cos(k*x(j)-2*pi*f*t(i))));
end
end
Nick

I feel like you could do all this one line, but I have no clue how.

For instance, I'm not sure if I'm defining x and t properly. Are they dependent on the way I define f and k?

x is dependent on the length of k which is dependent on the length of f, so it's really comes down to length of y as the input to the fft function. You can pad y with zeros, for instance, to make it longer.
 
  • #6
nickthequick said:
There are two things that are bothering me. For one, MATLAB has indexed my time axis backwards, so in order to plot my time values appropriately i have to make them negative. Is there an easy way to fix this?

flip the matrix:
http://www.mathworks.com/help/techdoc/ref/flipud.html

The second thing is that because this solution is periodic, I'm seeing artificial "noise" from the other solutions. Is there an easy way to remove these unwanted solutions so that I just display, in effect, the primary branch?

not sure what you're talking about exactly, but you can reduce noise by either smoothing (matlab's smooth function is god-awefully slow though, I'd suggest the fastsmooth on the open file exchange) or by just low-pass filtering (assuming the noise is a reasonably higher frequency than your data-of-interest).
 
  • #7
Thanks for the responses, Pythagorean.

My first problem was solved using set(gca,'YDir','normal');

As for the second problem, I'll try to quantify what I mean a bit more. I believe what we are observing in the previously appended plot are three solutions. Call them f(x,t), f(x,t-tau) and f(x,t+tau) where f(x,t) is the solution that I'm interested in and is the one that originates around t = 40-60 seconds. I believe that these other solutions, f(x,t-tau) and f(x,t+tau), are simply an artifact of the periodicity of solutions when using the Fourier transform. To me, these solutions represent noise and I would like to remove them so that all I have left is f(x,t).

I have been experimenting with creating filters in Fourier space to try and remove this unwanted information but so far have been unsuccessful.

As usual any help would be greatly appreciated!

Nick
 
  • #8
Well, filters will take out frequency components; but the way you've stated it, you're trying to remove temporal components? Is this correct?

you can move between temporal shifts and frequency via the "shift theorem"
 
  • #9
what i ended up doing was padding my initial signal with a sufficient number of zeros such that the period becomes extremely large - this way the unwanted periodic noise is no longer an issue in the domain that i care about.

thanks for all of your help,

nick
 
  • #10
nickthequick said:
The second thing is that because this solution is periodic, I'm seeing artificial "noise" from the other solutions. Is there an easy way to remove these unwanted solutions so that I just display, in effect, the primary branch?

Find out about using a "window function" on your raw data before you do the FFT. Two standard "general purpose" windows are called the Hamming and Hanning windows (invented by two different people with confusingly similar names!). There are others (Kaiser-Bessel, exponential, flat-top, etc, etc) but unless you want to get deep into this, either of the Hamming or Hanning should work pretty well and you probably won't see much difference between using one or the other.

It would be amazing if Matlab doesn't have these already implemented (but I don't use it much) - check the documentation.
 
  • #11
yeah MATLAB has a whole digital signal processing toolbox with it that includes windowing funcitons and such.
 

1. How is sea surface height measured?

Sea surface height is measured using altimetry, which involves sending radar or laser pulses from a satellite to the ocean's surface and measuring the time it takes for the pulse to return. This data is then used to calculate the sea surface height.

2. Why is FFT used to solve sea surface height in MATLAB?

FFT (Fast Fourier Transform) is used because it is a mathematical algorithm that allows for quick and efficient calculation of the spectrum of a signal. In the case of sea surface height, FFT is used to analyze the altimetry data and extract information about the ocean's surface.

3. What does the FFT output represent in terms of sea surface height?

The FFT output represents the amplitude and frequency components of the sea surface height signal. This means it shows the different heights and frequencies of the ocean's surface over a specific time period.

4. How does MATLAB handle noise in the sea surface height data?

MATLAB has built-in functions for filtering and removing noise from signals, including sea surface height data. These functions can be used to preprocess the data before applying the FFT algorithm to get a more accurate representation of the sea surface height.

5. Can FFT be used to predict future sea surface height changes?

Yes, FFT can be used to analyze past sea surface height data and make predictions about future changes. However, it is important to note that sea surface height is affected by many factors and cannot be predicted with complete accuracy.

Similar threads

  • Calculus and Beyond Homework Help
Replies
8
Views
245
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Classical Physics
Replies
5
Views
876
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Differential Equations
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Special and General Relativity
Replies
4
Views
1K
  • Differential Equations
Replies
11
Views
2K
Back
Top