Signal Processing – Apply Median Filters (Python)

The median filter is a nonlinear digital filtering technique, often used to remove noise. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image). Read more about the median filter here.

Here we want to implement a simple median filter in python. You need to install numpy, scipy, and matplotlib modules to replicate the code below. The filter has two inputs, first the input signal and a windows size (kernel size). Notice, the kernel size have to be an odd number.


import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy import signal

t = np.linspace(0,10,200) # create a time signal
x1 = np.sin(t) # create a simple sine wave
x2 = x1 + np.random.rand(200) # add noise to the signal
y1 = sp.signal.medfilt(x2,21) # add noise to the signal
# plot the results
plt.subplot(2,1,1)
plt.plot(t,x2,'yo-')
plt.title('input wave')
plt.xlabel('time')
plt.subplot(2,1,2)
plt.plot(range(200),y1,'yo-')
plt.title('filtered wave')
plt.xlabel('time')
plt.show()

figure_1

Posted in Linux, Machine Learning, Optimization, programming, Python, Robotics, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Linear vector mapping (scaling) – Python

The mathematics behind the linear mapping is presented in the previous post.

The equivalent function in MATLAB is posted here.

The following function is provided to be used in python programs. (check and correct the indenting of the function before usage). The following function uses numpy module, check the example after the function to see the correct usage.

def linmap(vin,rout):
# function for linear mapping between two ranges
# inputs:
# vin: the input vector you want to map, range [min(vin),max(vin)]
# rout: the range of the resulting vector
# output:
# the resulting vector in range rout
# usage:
# >>> v1 = np.linspace(-2,9,100);
# >>> rin = np.array([-3,5])
# >>> v2 = linmap(v1,rin);
# *** (this function uses numpy module)
#
a = np.amin(vin);
b = np.amax(vin);
c = rout[0];
d = rout[1];
return ((c+d) + (d-c)*((2*vin - (a+b))/(b-a)))/2;

How to use this function? check the following example:

>>> import numpy as np
>>> v1 = np.linspace(-2,9,20)
>>> rin = np.array([-3,5])
>>> linmap(v1,rin)
array([-3. , -2.57894737, -2.15789474, -1.73684211, -1.31578947,
-0.89473684, -0.47368421, -0.05263158, 0.36842105, 0.78947368,
1.21052632, 1.63157895, 2.05263158, 2.47368421, 2.89473684,
3.31578947, 3.73684211, 4.15789474, 4.57894737, 5. ])
Posted in Linux, Machine Learning, MATLAB, Optimization, programming, Python, Robotics, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Linear vector mapping (scaling) – MATLAB

Vector mapping means changing the range of a vector from range-1 to range-2. For instance, if a vector is in range [2,12] and we want to scale the vector and map it to range [0,2]. I give you some useful hints here:

Case-1: mapping from [0,1] to [a,b]:

if the vector is in range v1 ∈ [0,1] and we want to convert it to range v2 ∈ [a,b], the following formulation works:

v2 = a + (b-a) * v1

example:

v1 = rand(100,1);    %vector in range [0,1]
v2 = -2 + (3-(-2)).*v1;    %vector in range [-2,3]

Case-2: mapping from [a,b] to [0,1]:

if the vector is in range v1 ∈ [a,b] and we want to convert it to range v2 ∈ [0,1], the following formulation works:

v2 = (v1 – a) / (b-a)

example:

v1 = linspace(-5,2,100);    %vector in range [-5,2]
v2 = (v1-(-5)) ./ (2-(-5));    %vector in range [0,1]

Case-3: mapping from [-1,1] to [a,b]:

if the vector is in range v1 ∈ [-1,1] and we want to convert it to range v2 ∈ [a,b], the following formulation works:

v2 = ((b+a) + (b-a) * v1) / (1-(-1))

example:

v1 = linspace(-1,1,100);                              % vector in range [-1,1]
v2 = (4-2)/2 + ((4-(-2))/(1-(-1))).*v1;    % vector in range [-2,4]

Case-4: mapping from [a,b] to [-1,1]:

if the vector is in range v1 ∈ [a,b] and we want to convert it to range v2 ∈ [-1,1], the following formulation works:

v2 = [(1-(-1))*v1 - (a+b)]  /  (b-a)

example:

v1 = linspace(-2,4,100);                                  %vector in range [-2,4]
v2 = ((1-(-1))*v1 - (-2+4)) / (4-(-2));          %vector in range [-1,1]

Case-5: mapping from [a,b] to [c,d]:

In the most general case, if the vector is in range v1 ∈ [a,b] and we want to convert it to range v2 ∈ [c,d], the conversion can be seen as a mapping from the first vector to [-1,1] and then mapping from [-1,1] to [c,d]. the following formulation works:

v1 ∈ [a,b] → [-1,1] → v2 ∈ [c,d]

v2 = [  (c+d) + (d-c)* [(2*v1 - (a+b)) / (b-a)]  ] / 2

example:

v1 = linspace(-2,9,100);                                                             %vector in range [-2,9]
v2 = 0.5* ((-5+3) + (3-(-5))*((2*v1 - (-2+9))/(9-(-2))) );  %vector in range [-5,3]

Matlab Function for linear mapping from [a,b] to [c,d]:

function vout = linmap(vin,rout)
% function for linear mapping between two ranges
% inputs:
% vin: the input vector you want to map, range [min(vin),max(vin)]
% rout: the range of the resulting vector
% output:
% vout: the resulting vector in range rout
% usage:
% >> v1 = linspace(-2,9,100);
% >> v2 = linmap(v1,[-5,5]);
%
a = min(vin);
b = max(vin);
c = rout(1);
d = rout(2);
vout = ((c+d) + (d-c)*((2*vin - (a+b))/(b-a)))/2;
end

The equivalent function in python is presented in this post.

Posted in Linux, Machine Learning, MATLAB, Optimization, programming, Robotics, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Minimum CMake file for YARP – A Small Tutorial

The following CMakeLists.txt file is the minimum requirement to be used in compiling codes written in YARP:

cmake_minimum_required(VERSION 2.8)
project(myYARPproject)
find_package(YARP)
include_directories(${YARP_INCLUDE_DIRS})
add_executable(myfile main.cpp)
target_link_libraries(myfile ${YARP_LIBRARIES})
install(TARGETS myfile RUNTIME DESTINASION ${CMAKE_CURRENT_SOURCE_DIR}/../bin)

 

Posted in C/C++, Linux, programming, Software, Ubuntu, yarp | Tagged , , , , , , , , , , , , , , , , , , , , | Leave a comment

CMAKE – tips & tricks

for pointing at the source directory in the cmake file you can use the parameter :

CMAKE_CURRENT_SOURCE_DIR

usage in the CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8)

project(hello)

message(STATUS, "Running from ${CMAKE_CURRENT_SOURCE_DIR}")

add_executable(hello hello.cpp)

 

 

 

Posted in C/C++, Linux, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Implement and solve ODE problems in MATLAB – Part 1

This example illustrates the steps for solving an initial value ODE problem. Consider we have the following set of equations:

\dot{x_1} = x_2 \\ \dot{x_2} = -0.1x_2 - x_1^3 +12cos(t)+u(t)

first of all we have to implement the equations into a MATLAB function. This is very easy as you can see:

function dxdt = model(t,x)
u = 0;     % we consider the input value equal to zero to see the natural behavior of the system
dxdt = [x(2); (-0.1*x(2)-x(1)^3+12*cos(t)+u)];
end

In order to solve the set of equations we can use the ODE45 solver as follows:

function solveMyProblem
[t,x] = ode45(@model,[0 60],[2; 2]);
figure;plot(x(:,1),x(:,2));xlabel('x_1');ylabel('x_2');
figure;subplot(1,2,1);plot(t,x(:,1));xlabel('t');ylabel('x_1');
subplot(1,2,2);plot(t,x(:,2));xlabel('t');ylabel('x_2');
end

The equation has been solved for the time span [0 60]secs with the initial values of [2;2]. The result can be seen in the following figures:

untitled1 untitled2

Posted in Linux, Machine Learning, MATLAB, Optimization, programming, Robotics, SIMULINK, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Extract data from ROS bags

If you have ROS-Bags and you want to extract data from it to work later on, you can do this:

rostopic echo "topic_name" -b "bag_name" -p > "file.csv"

 

for instance:

rostopic echo /turtle1/pose -b bag1.bag -p > file.csv

 

explanation: The rostopic command uses the echo option to print the output of a topic on the screen. So if you do something like:

rostopic echo "topic_name"

 

You will see the message of the topic, if it is publishing the data. In order to publish from a bag file and not from a live topic you add the following part:

rostopic echo "topic_name" -b "bag_name"

 

and the last part just directs the output from the screen to the csv file.

Posted in Linux, programming, Robotics, ROS, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , | Leave a comment