Boot repair

Boot-Repair can be installed & used from any Ubuntu session (normal session, or live-CD, or live-USB).

Install Boot-Repair on ubuntu

Open the terminal and run the following commands

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install boot-repair

After completing the installation you can launch it from System->Administration->Boot-Repair menu if you use Gnome, or search “boot-repair” in the dash if you use Unity. Then follow the menus

Screenshots



Posted in Linux | 1 Comment

Display multiple figures using matplotlib

display multiple windows of figures for plotting, put show() out of loop!

otherwise, only one will be displayed once unless.

Tagged , | Leave a comment

Change Linux mint 32bit to 64bit version

1. Download iso file;

2. use imagewriter which is a default software of mint to write iso to usb stick;

3. boot from usb and enter the mint system to install

4. start installation. Choose partition where the old linux system is located. This partition has to be formated with ext4 file format. root directory choose /

5. continue.

if booter is not successfully installed,  the system can not be started with information :unknown file system:

then enter the os in usb and install boot-repair to fix this problem:

sudo add-apt-repository ppa:yannubuntu/boot-repair 
sudo apt-get update 
sudo apt-get install boot-repair
Posted in Linux | Leave a comment

Find & Replace across multiple files in linux

Find & Replace across multiple files in linux

I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

  1. find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:
    ./file.php
    ./includes/test.php
    ./classes/class.php
  2. xargs– This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
    arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
  3. sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit.  In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.

Resources:

Posted in Linux | Leave a comment

signal smooth filtering (include moving average algorithm)

import numpy

def smooth(x,window_len=5,window=’hanning’):

“””smooth the data using a window with requested size.

This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.

input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’
flat window will produce a moving average smoothing.

output:
the smoothed signal

example:

t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)

see also:

numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter

TODO: the window parameter could be the window itself if an array instead of a string
NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
“””

if x.ndim != 1:
raise ValueError, “smooth only accepts 1 dimension arrays.”

if x.size < window_len:
raise ValueError, “Input vector needs to be bigger than window size.”

if (window_len/2.0-int(window_len/2.0))==0:
raise ValueError, “Window length is not a odd number.”

if window_len<3:
return x
if not window in [‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’]:
raise ValueError, “Window is on of ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman'”

print(window_len)
s=numpy.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]

xDim_temp = x.shape
xDim = xDim_temp[0]

#print(len(s))
if window == ‘flat’: #moving average
w=numpy.ones(window_len,’d’)
else:
w=eval(‘numpy.’+window+'(window_len)’)

y=numpy.convolve(w/w.sum(),s,mode=’valid’)

yret = numpy.zeros(xDim)
for i in range(0,xDim):
yret[i] = y[i+(window_len-1)/2]
return yret

Posted in Fundamental | 2 Comments

Savitzky Golay Filtering . py

A useful filter for signal smoothing

def savitzky_golay(y, window_size, order, deriv=0, rate=1):
r”””Smooth (and optionally differentiate) data with a Savitzky-Golay filter.
The Savitzky-Golay filter removes high frequency noise from data.
It has the advantage of preserving the original shape and
features of the signal better than other types of filtering
approaches, such as moving averages techniques.
Parameters
———-
y : array_like, shape (N,)
the values of the time history of the signal.
window_size : int
the length of the window. Must be an odd integer number.
order : int
the order of the polynomial used in the filtering.
Must be less then `window_size` – 1.
deriv: int
the order of the derivative to compute (default = 0 means only smoothing)
Returns
——-
ys : ndarray, shape (N)
the smoothed signal (or it’s n-th derivative).
Notes
—–
The Savitzky-Golay is a type of low-pass filter, particularly
suited for smoothing noisy data. The main idea behind this
approach is to make for each point a least-square fit with a
polynomial of high order over a odd-sized window centered at
the point.
Examples
——–
t = np.linspace(-4, 4, 500)
y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)
ysg = savitzky_golay(y, window_size=31, order=4)
import matplotlib.pyplot as plt
plt.plot(t, y, label=’Noisy signal’)
plt.plot(t, np.exp(-t**2), ‘k’, lw=1.5, label=’Original signal’)
plt.plot(t, ysg, ‘r’, label=’Filtered signal’)
plt.legend()
plt.show()
References
———-
.. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of
Data by Simplified Least Squares Procedures. Analytical
Chemistry, 1964, 36 (8), pp 1627-1639.
.. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing
W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
Cambridge University Press ISBN-13: 9780521880688
“””
import numpy as np
from math import factorial

try:
window_size = np.abs(np.int(window_size))
order = np.abs(np.int(order))
except ValueError, msg:
raise ValueError(“window_size and order have to be of type int”)
if window_size % 2 != 1 or window_size < 1:
raise TypeError(“window_size size must be a positive odd number”)
if window_size < order + 2:
raise TypeError(“window_size is too small for the polynomials order”)
order_range = range(order+1)
half_window = (window_size -1) // 2
# precompute coefficients
b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
# pad the signal at the extremes with
# values taken from the signal itself
firstvals = y[0] – np.abs( y[1:half_window+1][::-1] – y[0] )
lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] – y[-1])
y = np.concatenate((firstvals, y, lastvals))
return np.convolve( m[::-1], y, mode=’valid’)

# Sample Below
import numpy as np
import pylab as plt
from math import factorial
t = np.linspace(-4,4,num=500)
y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)
ysg = savitzky_golay(y, window_size=31, order=4)

plt.plot(t, y, label=’Noisy signal’)
plt.plot(t, np.exp(-t**2), ‘k’, lw=1.5, label=’Original signal’)
plt.plot(t, ysg, ‘r’, label=’Filtered signal’)
plt.legend()
plt.show()

Posted in Fundamental | Tagged | Leave a comment

Signal Smoothing

Source: http://www.chem.uoa.gr/applets/appletsmooth/appl_smooth2.html

Signal Smoothing Algorithms

 

Theory

The signal-to-noise ratio (SNR) of a signal can be enhanced by either hardware or software techniques. The wide use of personal computers in chemical instrumentation and their inherent programming flexibility make software signal smoothing (or filtering) techniques especially attractive. Some of the more common signal smoothing algorithms described below.

 

Moving average algorithm

The simpler software technique for smoothing signals consisting of equidistant points is the moving average. An array of raw (noisy) data [y1, y2, …, yN] can be converted to a new array of smoothed data. The “smoothed point” (yk)s is the average of an odd number of consecutive 2n+1 (n=1, 2, 3, ..) points of the raw data yk-n, yk-n+1, …, yk-1, yk, yk+1, …, yk+n-1, yk+n, i.e.

   

The odd number 2n+1 is usually named filter width. The greater the filter width the more intense is the smoothing effect. This operation is depicted in the animated picture below.

  

In this example the filter width is 5. The first five raw data (black squares) within the red rectangle (moving window) are averaged and their average value is plotted as smoothed (green squares) data point 3. The rectangle is then moved one point to the right and points 2 through 6 are averaged, and the average is plotted as smoothed data point 4, and so on. This procedure is called a 5-point unweighted smooth.

The signal-to-noise ratio may be further enhanced by increasing the filter width or by smoothing the data multiple times. Obviously after each filter pass the first n and the last n points are lost.

The results of this technique are deceptively impressive because of excessive filtering. Actually, information is lost and/or distorted because too much statistical weight is given to points that are well removed from the central point. Moving average algorithm is particularly damaging when the filter passes through peaks that are narrow compared to the filter width.

 

Savitzky-Golay algorithm

A much better procedure than simply averaging points is to perform a least squares fit of a small set of consecutive data points to a polynomial and take the calculated central point of the fitted polynomial curve as the new smoothed data point.

Savitzky and Golay (see A. Savitzky and M. J. E. Golay, Anal. Chem., 1964, 36, 1627) showed that a set of integers (A-n, A-(n-1) …, An-1, An) could be derived and used as weighting coefficients to carry out the smoothing operation. The use of these weighting coefficients, known as convolution integers, turns out to be exactly equivalent to fitting the data to a polynomial, as just described and it is computationally more effective and much faster. Therefore, the smoothed data point (yk)s by the Savitzky-Golay algorithm is given by the following equation:

  

Many sets of convolution integers can be used depending on the filter width and the polynomial degree. Typical sets of these integers for “quadratic smooth” are shown in the table below:

 

 

Filter width (2n+1)
i 11 9 7 5
-5 -36
-4 9 -21
-3 44 14 -2
-2 69 39 3 -3
-1 84 54 6 12
0 89 59 7 17
1 84 54 6 12
2 69 39 3 -3
3 44 14 -2
4 9 -21
5 -36

Sets of convolution integers can be used to obtain directly, instead of the smoothed signal, its 1st, 2nd, …, mth order derivative, therefore Savitzky-Golay algorithm is very useful for calculating the derivatives of noisy signals consisting of descrete and equidistant points.

The smoothing effect of the Savitzky-Golay algorithm is not so aggressive as in the case of the moving average and the loss and/or distortion of vital information is comparatively limited. However, it should be stressed that both algorithms are “lossy”, i.e. part of the original information is lost or distorded. This type of smoothing has only cosmetic value.

 

Ensemble Average

In ensemble average successive sets of data are collected and summed point by point. Therefore, a prerequisite for the application of this method is the ability to reproduce the signal as many times as possible starting always from the same data point, contrary to the previous two algorithms which operate exclusively on a single data set.

Typical application of ensemble average is found in NMR and FT-IR spectroscopy, where the final spectrum is the result of averaging thousands of individual spectra. This is the only way to obtain a meaningful signal, when a single scan generates a practically unreadable signal heavily contaminated with random noise.

Repetitive additions of noisy signals tend to emphasize their systematic characteristics and to cancel out any zero-mean random noise. If (SNR)o is the original signal-to-noise ratio of the signal, the final (SNR)f after N repetitions (scans) is given by the following equation:

  

Therefore, by averaging 100 (or 1000) data sets a 10-fold (or a 100-fold) reduction of noise level is achieved.

 

Applet

This applet is a demonstration of the aforementioned signal smoothing algorithms. The user has several options. Up to 4 different types (F1, F2, F3, F4) of signals consisting of 1000 data points can be selected. Up to 3 levels of zero-mean normally distributed random noise can be added to the normal (noiseless) signal. 3 filter widths (5, 7, 9 points) can be selected for the moving average and Savitzky-Golay algorithms.

The smoothed signal appears in black color, whereas the user has the option to allow the normal (noiseless) signal and/or the original noisy signal to be displayed in the plot area with faint blue and red colors, respectively, for comparison.

It is of interest to observe the difference between moving average and Savitzky-Golay algorithms using the signal F3. This signal consists of seven Gaussian peaks of equal height with progressively decreasing width. By successive applications (passings) of the moving average the peak-height of the more narrow peaks decreases, i.e. a crucial information (the height) is distorted. In sharp contrast to moving average, the Savitzky-Golay algorithm “respects” this information and the degradation of the signal is limited.

The ensemble average procedure is best demonstrated with the signal F2, that simulates the very narrow peaks encountered in NMR spectra. By adding high level noise, this signal becomes almost unreadable and only the higher peaks are discernible. By applying ensemble average and by increasing the number of scans, the signal gradually emerges and even the smaller peaks can be safely recognized.

 

 

Posted in Fundamental | 1 Comment

Music I like

许飞

寻水的鱼

我们终究会牵手旅行

明年依然好时节

Tagged | Leave a comment

Software list in linux

Linux OS: Linux Mint (Cinnamon), Ubuntu

File manager (mainly for synchronizing files ): nemo

Avant Window Navigator: good dock

GTK+theme: Mediterranean /usr/share/themes/

Image editor : GIMP;  Inkscape (vector-based);

Screen capture: Ksnapshot; (there is a default one, but not as good as this guy)

Play on Linux is required to install microsoft office under Linux OS.

TexMaker & Kile & xdvi for Latex; texlive-publishers.

Media player: always VLC

Sound recorder: audacity; Perfect and professional software for recording sound.

Gmount is for virtual cdrom iso;

Python, numpy, scipy, matplotlib… Good enough alternative for matlab

Python coding: spe / geany;

version control: git

synchronize: rsync

imagewrite for writing iso files to usb stick or CD. Very good tool

linux cinnamon support multitouch.

 

 

 

Posted in Linux | Leave a comment

Cisco VPN problem during installation

Problem: Cisco Anyconnect client does not give an option to override the “invalid certificate error” and go ahead using the current certificate of the server.

Reason: The current libraries used by Anyconnect client does not support this feature.

Fix: Add libraries from Firefox, as below:

  1. If firefox is not installed, install firefox from Ubuntu Software Center.
  2. On terminal run: sudo cp /usr/lib/firefox/lib*.so /opt/cisco/vpn/lib/
  3. Restart Anyconnect client and connect to required host. The certificate acceptance prompt should appear now.
Posted in Linux | 2 Comments