Tuesday, December 13, 2016

FFT with window normalization: python implementation




We consider the harmonic function of a field as follows:

           x = amp* sin(2*pi*f*t)

Here, amp is the amplitude of the field, i.e, displacement or pressure, f is the frequency of the harmonic signal and the t is the time (parameter).

If we want to deal with RMS of the sinusoid signal than ampl/sqrt(2) should be used. Before performing Discrete Fourier Transform it is good practice to window signal in order to decreases leakage, etc.

Transforming the harmonic signal into frequency domain, two-sided spectrum is obtained. Normalization should be done done by dividing values of magnitude by energy of the window. If we to take signal "as it is" (which is equal to applying rectangular window) the spectrum is then divided by number of samples N. For different types of windows this factor that is equal to sum of all window samples.

Additionally we are only interested in one half of a spectrum, therefore amplitude of all samples must be multiplied by two to compensate the loss of energy, except of DC component which appears only once.

So the python code is as follows.


import numpy as np
import matplotlib.pyplot as plt
from scipy import signal as mySignal
from scipy.signal import argrelextrema

plt.close('all')

f1 = 1
f2 = 2
f3 = 3
T1 = 1/f1
T2 = 1/f2
T3 = 1/f3

fs = f1*20
duration = 1/f3*8
npts = int(fs*duration)
t = np.arange(npts, dtype=float)/fs

amp1    = 1
amp2    = 2
amp3    = 3

signal  = amp1 * np.sin((f1*duration) * np.linspace(0, 2*np.pi, npts))  + amp2 * np.sin((f2*duration) * np.linspace(0, 2*np.pi, npts))  + amp3 * np.sin((f3*duration) * np.linspace(0, 2*np.pi, npts))
signal3 =amp3 * np.sin((f3*duration) * np.linspace(0, 2*np.pi, npts))

plt.figure(1)
plt.subplot(2,1,1)
plt.plot(t, signal)



# Window signal
win_hamming = np.hamming(npts)
win_boxcar  = mySignal.boxcar(npts)
win = win_boxcar
#win = win_hamming

signal = signal * win
plt.plot(t, signal)
plt.plot(t, signal3,linewidth=10)
plt.title("Boxcar window")
plt.ylabel("Amplitude")
plt.xlabel("Time,s")

sp = np.fft.fft(signal)
freq = np.fft.fftfreq(npts, 1.0/fs)

# Scale the magnitude of FFT by window energy and factor of 2,
# because we are using half of FFT.
sp_mag = np.abs(sp) * 2 / np.sum(win)

# To obtain RMS values, divide by sqrt(2)
sp_rms = sp_mag  / np.sqrt(2)

# Shift both vectors to have DC at center
freq   = np.fft.fftshift(freq)
sp_rms = np.fft.fftshift(sp_rms)

x = sp_rms*np.sqrt(2.0)
m = argrelextrema(x, np.greater)   #array of indexes of the locals maxima

freq_local   = [freq[m] for i in m]
sp_rms_local = [sp_rms[m]*np.sqrt(2.0) for i in m]

print(freq_local)
print(sp_rms_local)

plt.subplot(2,1,2)
plt.plot(freq, sp_rms*np.sqrt(2.0))
plt.plot(freq_local, sp_rms_local, 'rs')
plt.xlim( (0, f1*2)  )
plt.grid('on')

plt.show()



Monday, November 21, 2016

Symmetry in Mathematics and in Physics





The proposition is that the symmetries of physics are a subset of the symmetries of mathematics.

Typically, a physical law satisfies a particular symmetry if it holds despite a change in some parameter of the set of phenomena covered by the law.
  1.  An obvious symmetry is invariance with respect to space which dictates that if the location at which an experiment is performed is changed the results of the experiment will nonetheless be the same. 
  2. Another symmetry, invariance with respect to time, mandates that the results of an experiment will stay the same even if the time that an experiment is performed changes. 
Physicists have generalized the term “symmetry” from descriptions of objects to descriptions of laws of nature. A law of nature exhibits symmetry when we can transform the phenomena under the scope of the law in certain ways and still make use of the same law to get a correct result. We say that such a law is “invariant” with respect to those transformations.

In 1918 symmetry became even more relevant to (the philosophy of) physics when Emmy Noether proved a celebrated theorem that connected symmetry to the conservation laws that permeate physics. The theorem states that for every continuous symmetry of the laws of physics, there must exist a related conservation law. Furthermore, for every conservation law, there must exist a related continuous symmetry.

  1. For example, the fact that the laws of physics are invariant with respect to space corresponds to conservation of linear momentum.The law says that within a closed system the total linear momentum will not change and the law is “mandated” by the symmetry of space. 
  2. Time invariance corresponds to conservation of energy. 
  3. Orientation invariance corresponds to conservation of angular momentum

Noether’s theorem had a profound effect on the workings of physics. Whereas physics formerly first looked for conservation laws, it now looked for different types of symmetries and derived the conservation laws from them. Increasingly, symmetries became the defining factor in physics.


Physics also respects another symmetry, which as far as we know has not been articulated as such. The symmetry we refer to is similar to the symmetry of time and place that was obvious for millennia but not articulated until the last century. Namely, a law of physics is applicable to a class of physical objects such that one can exchange one physical object of the appropriate type for another of that type with the law remaining the same. Consider classical mechanics. The laws for classical mechanics work for all medium sized objects not moving close to the speed of light. In other words, if a law works for an apple, the law will also work for a moon. Quantities like size and distance must be accounted for, but when a law is stated in its correct form, all the different possibilities for the physical entities are clear, and the law works for all of them. We shall call this invariance for a law of nature, symmetry of applicability, i.e. a law is invariant with respect to exchanging the objects to which the law is applied.


To sum up our main point, philosophically the change in the role of symmetry has been revolutionary. Physicists have realized that symmetry is the defining property of laws of physics. In the past, the “motto” was that a law of physics respects symmetries. In contrast, the view since Einstein is: that which respects symmetries is a law of physics. In other words, when looking at the physical phenomena, the physicists picks out those those that satisfy certain symmetries and declares those classes of phenomena to be operating under a law of physics. Stenger summarizes this view as follows:
             “. . . the laws of physics are simply restrictions on the ways physicists may draw the models they use to represent the behavior of matter”. !!!

They are restricted because they must respect symmetries. From this perspective, a physicist observing phenomena is not passively taking in the laws of physics. Rather the observer plays an active role. She looks at all phenomena and picks out those that satisfy the requisite symmetries.

Our main point is that this uniform transformation and the fact that statements remain true under such a transformation is a type of symmetry. Recall, a symmetry allows us to change or transform an object or “law” and still keep some vital property invariant.

As with physics, in the past whereas we used to understand that:
           "A mathematical statement satisfies symmetry of semantics".
We now claim that:
           "A statement that satisfies symmetry of semantics is a mathematical statement."
In other words, given the many expressible statements a mathematician finds, her job is to choose and organize those that satisfy symmetry of semantics.





Tuesday, November 8, 2016

Fluent compilation error: 'cl' is not recognized as an internal or external command

In the case you get the error

'cl' is not recognized as an internal or external command,
operable program or batch file.
NMAKE :  U1077: 'cl' : return code '0x1'
Stop.

Install the C++ tools since they are not being installed by default in Visual Studio 2015. Run the setup again, and install the C++ 



Wednesday, November 2, 2016

Shared folders in Linux Guest on Windows Host

This issue occurs if the GCC and PAE kernels are not up to date in the Linux VM and the hgfs folder is not created in the /mnt folder.

  1. su - 
  2. yum install gcc gcc-c++ make binutils
  3. yum update kernel
  4. reboot
  5. ./vmware-install.pl
  6. Power on the virtual machine. The shared folder should be accessible in the given location, /mnt/hgfs/.

    Accessing your Virtualbox Guest from your Host OS

    Wednesday, September 28, 2016

    Linux I/O Performance Tests using dd

    dd if=/dev/zero of=test2.img bs=164K count=100000 oflag=dsync
    dd bs=1M count=1024 if=/dev/zero of=test conv=fdatasyn


    https://romanrm.net/dd-benchmark

    Thursday, September 22, 2016

    Install Htop 2.0 for CentOS 7

    yum groupinstall "Development Tools"
    yum install ncurses ncurses-devel
    wget http://hisham.hm/htop/releases/2.0.0/htop-2.0.0.tar.gz
    tar xvfvz htop-2.0.0.tar.gz
    cd htop-2.0.0
    ./configure
    make
    make install

    Monday, March 21, 2016

    Comsol: Tuning the Java heap size

    If you get an error message like this: 
                    Error in sweep: Java heap space
    try to increase the Java heap. Open the comsol.ini file located in the subdirectory bin/$arch in the COMSOL installation directory, where $arch corresponds to the platform you are using. Modify the number in the row that starts with -Xmx, its value is the Java heap size. If you have enough RAM, try doubling the size. This will give the line -Xmx2048m for a 64-bit platform. Repeat this for all *.ini files in the same directory.

    If you do not have write permission to the bin/$arch directory, you can instead try the following:
    • Copy the comsol.ini file to a folder where you have write permission.
    • Open the file and modify it according to the instructions above.
    • When you launch COMSOL, add the option -comsolinifile [path] to the COMSOL command, where [path] is the path to your new comsol.ini file.


    Also here we can find some recommendations on how to deal with java.lang.OutOfMemoryError in Java, http://javarevisited.blogspot.co.il/2011/09/javalangoutofmemoryerror-permgen-space.html

    Wednesday, January 6, 2016

    Color schemes - Midnight Commander


    [Colors]
    base_color=normal=lightgray,black:input=white,brightred:errors=white,brightred:gauge=yellow,red:selected=gray,lightgray:marked=yellow,black:markselect=yellow,brown:directory=white,black:executable=brightgreen,black:link=green,black:device=brightmagenta,black:special=brown,black:core=brightred,yellow:menu=gray,lightgray:menuhot=yellow,lightgray:menuhotsel=brightred,black:dnormal=white,gray:dfocus=white,brightgreen:dhotnormal=brightred,gray:dhotfocus=brightred,gray:editmarked=white,brightred:editnormal=white,black:editbold=blue,brown:reverse=yellow,black:helpnormal=white,black:helpitalic=brightmagenta,black:helpbold=brightcyan,black:helpslink=yellow,black:helplink=brightblue,black:viewunderline=brightgreen,white

    How to Encrypt and Decrypt Files With GPG on Linux

    https://www.howtogeek.com/427982/how-to-encrypt-and-decrypt-files-with-gpg-on-linux/