Friday, April 28, 2017
Detected EEG wave that can tell whether eyes are open or closed
Tuesday, April 25, 2017
My first ECG signal recording
Here is the system structure:
human body -> electrodes -> leads -> ADS1298 -> serial port -> PC -> C# app -> matlab
Friday, November 4, 2016
How to let SharpGL capturing KeyDown event?
- SharpGL won't capture KeyDown event
- Make OpenGLControl focusable, like this: Focusable="True"
- Set focus on the OpenGL control in the hosting window: OpenGLControl.Focus();
Thursday, September 29, 2016
Solution for: (Visual Studio 2015)Error while trying to run project: Unable to start debugging
Issue: When you trying to debug the application, VS says "Error while trying to run project: Unable to start debugging".
Solution: Delete your .suo file of your solution.
Sunday, June 26, 2016
Convert MIT-BIH Polysomnographic data to mat (Matlab) format
https://www.physionet.org/physiotools/wfdb.shtml, click it. Find "Ready-to-run, precompiled binaries" https://www.physionet.org/physiotools/binaries/. OK, in this page you will find the tool in binary format. I use windows, so I downloaded the windows version.
Here is a command line example:
bin\wfdb2mat -r slpdb/slp14 -f 0 -t 21600 -s 2 >slp14m.info
Make sure you have .hea file along with .dat file. The last command will produce slp14m.mat and other stuffs. slp14m.mat is just you want. Try to load it into matlab:
eegdata = load('slp14m.mat');
All are done!
Tuesday, May 17, 2016
Digital Elliptic Filter in Matlab
The elliptic filter works pretty well, so efficient. The following filter will band 50Hz and near frequencies (see the third figure). And the last figure shows the filtered signal.
==== m code started ====
clear
Rp = 5; % peak-to-peak passband ripple, the smaller the smoother
Rs = 60; % stopband attenuation
Fs = 250; % sample rate
Wp = [47, 53]/Fs*2; % passband edge frequency
Ws = [48, 52]/Fs*2; % stopband edge frequency
[N, Wn] = ellipord(Wp, Ws, Rp, Rs); % find the minimum order of the required filter
fprintf('Wn %f, %f\n', Wn(1)*Fs/2, Wn(2)*Fs/2);
[b,a] = ellip(N,Rp,Rs, Wn,'stop'); % design an elliptic filter
freqz(b,a); % draw frequency response of the filter
title(['ellipord Analog bandstop filter order N=',num2str(N)]);
xlabel('Frequency(Hz)');
tseq = 0: 1/Fs :6;
dataIn = sin(tseq*10 *2*pi) + sin(tseq*50 *2*pi) + sin(tseq*52 *2*pi) + sin(tseq*48 *2*pi);
dataOut = filter(b,a,dataIn);
figure(2);
plot(dataIn);
figure(3);
plot(dataOut);
Monday, January 18, 2016
vector::operator [] is time consuming
vector
while( _all_elements_in_arr_)
{
arr[i] = x; // it's extremely slow.
}
So avoid of using operator [], instead, use direct address of the buffer could improve the performance tremendously:
double * buffer_addr = &arr[0];
Friday, December 18, 2015
[IDL 2 CPP] Let Bison/Flex Generate C++ Parser
Download Win Flex Bison
I tried two windows versions. This is the good one to support C++ parser.
http://sourceforge.net/projects/winflexbison/
Option for Bison
These two options tell Bison to generate a C++ parser. The file lalr1.cc is in the win_flex_bison downloaded package.
%language "c++"
%skeleton "lalr1.cc"
Setup Parser's Class Name
The following option give a name to the parser class.
%define parser_class_name "idl_parser"
The parser class would look like as following
/// A Bison parser.
class idl_parser
{
idl_parser (yy::IdlTranslator& translator_yyarg, yy::IdlScanner& scanner_yyarg, std::ifstream* arg_yyin_yyarg, std::ofstream* outputStream_yyarg);
All arguments passed in, the parser will store them in member variables.
/* User arguments. */
yy::IdlTranslator& translator;
yy::IdlScanner& scanner;
std::ifstream* arg_yyin;
std::ofstream* outputStream;
Tell the Parser the Prototype of the yylex
"yylval" is used to return token string from scanner to parser. "yylloc" is an argument which could tell the current location in the source file. This parameter is enabled by %locations . If you didn't use %locations, then there would be no yylloc.
The following macro definition usually be defined in scanner header file. And included by bison .y file and flex .l file.
#ifndef YY_DECL
# define YY_DECL \
yy::idl_parser::token_type \
yy::IdlScanner::lex(yy::idl_parser::semantic_type* yylval, yy::idl_parser::location_type *yylloc)
#define yylex scanner.lex
The parser will call lex as following
YYCDEBUG << "Reading a token: ";
yychar = yylex (&yylval, &yylloc);
By defining the yylex macro, parser actually calls scanner.lex().
Implementing the Scanner
Pay attention to the first part of the following code please. It redefines yyFlexLexer, so the class yyFlexLexer in file "FlexLexer.h" becomes IdlFlexLexer. It very important, it avoids many conflicts. But it's really a poor technique.
The following is part of the scanner's header file.
#ifndef __FLEX_LEXER_H
#define yyFlexLexer IdlFlexLexer
#include "FlexLexer.h"
#undef yyFlexLexer
#endif
#include "idl.tab.hh"
namespace yy
{
class IdlScanner : public IdlFlexLexer
{
public:
IdlScanner(std::ifstream* arg_yyin);
virtual ~IdlScanner();
virtual idl_parser::token_type lex(
idl_parser::semantic_type* yylval,
yy::idl_parser::location_type *yylloc
);
};
}
Have a Look at the Implementation of IdlScanner::lex()
The file lex.???.cc is the lexer implementation file. The file is generated by flex. In this file, the macro YY_DECL just defined before, is used here for the implementation code.
YY_DECL
{
register yy_state_type yy_current_state;
register char *yy_cp, *yy_bp;
register int yy_act;
...
How does the Input File Passed into Scanner
The scanner class is derived from class yyFlexLexer which is defined in FlexLexer.h. The input file "std::ifstream* in" is actually passed to the parent class via ctor. Remember yyFlexLexer was defined as IdlFlexLexer.
IdlScanner::IdlScanner(std::ifstream* in)
: IdlFlexLexer(in)
{
}
The implementation of yyFlexLexer ctor is in file lex.???.cc, which is generated by flex. The input file arg_yyin is stored in yyin, a member variable of yyFlexLexer. yyin is used to be a global variable in C lexer version. If yyin is NULL, then the lexer would use stdin as input.
yyFlexLexer::yyFlexLexer( std::istream* arg_yyin, std::ostream* arg_yyout )
{
yyin = arg_yyin;
yyout = arg_yyout;
yy_c_buf_p = 0;
yy_init = 0;
yy_start = 0;
yy_flex_debug = 0;
yylineno = 1; // this will only get updated if %option yylineno
First create the scanner with the input file, and then pass the scanner to the parser. When you call parser.parse(), the parser will call scanner.lex() to get tokens one by one.
std::ifstream *inputFile = new std::ifstream();
inputFile->open("some source file here");
std::ofstream *outputFile = new std::ofstream();
outputFile->open("some output file here");
IdlScanner scanner(inputFile);
idl_parser parser(*this, scanner, inputFile, outputFile);
int parse_ret = parser.parse();
Sunday, November 29, 2015
[IDL 2 CPP] Implement Variable Definitions
The basic method of adding declarations is by using mid-rules in Bison grammar file.
As following, initialize a data structure that could store variable names, and then each time the parse meets an assignment statement, store the variable name for future use. The following code show the way of recording variable names. But its not accurate, because unary_expression doesn't equal to variable name, sometimes it's an element of an array or some other ting. So here we need more complex code logic to accomplish it. But I won't put these code here because here is just a show of high level of structure of the method.
assignment_statement:
unary_expression '=' expression
{printf("bison got assign statement: %s = %s\n", $1, $3);
VariableNameSet_TryAdd($1);
$$ = AllocBuff();
sprintf_s($$, TEXT_BUFFER_LEN, "%s = %s;", $1, $3);
}
;
KEY_FUNCTION identifier parameter_list_line
{
VariableNameSet_Init();
}
statement_list KEY_END end_of_line
{
char *buf = AllocBuff();
IncreaseTab($5, buf);
$$ = AllocBuff();
char *var_decl_buf = AllocBuff();
VariableNameSet_DecleareVariables(var_decl_buf);
char *var_decl_buf_tabbed = AllocBuff();
IncreaseTab(var_decl_buf, var_decl_buf_tabbed);
sprintf_s($$, TEXT_BUFFER_LEN, "Variant %s%s{\n%s%s}%s\n", $2, $3, var_decl_buf_tabbed, buf, $7);
printf("IDL function: [%s]\n", $$);
}
;
Saturday, November 28, 2015
[IDL 2 CPP] Translate IDL Array Subscript Ranges to C++
Since the original data processing program which was written in IDL is so large, it's impossible to translate manually, I decide to develop a translator then translate it into C++ source code. I've worked on it for a couple of weeks. And today I finished the ranged array part. I think it's interesting so I'm going to share the development experience for you.
I am using flex/bison to generate a parser. The grammar rules for IDL's array and subscripts are here(ask me for sample code):
postfix_expression:
primary_expression
| postfix_expression '[' array_subscripts ']'
array_subscripts:
array_subscript
| array_subscripts ',' array_subscript
;
array_subscript:
expression
| range_or_whole_range ':' range_or_whole_range
;
range_or_whole_range:
expression
| '*'
;
No action codes here yet. These grammar rules cover simple array subscripts which will visit an element of an array, and also the subscript ranges case. For the simple case, the IDL code is easy to translate. For example, IDL code:
a = b[1]
which would be translated to
a = b[1];
Just exactly the same code in C++. But it would be more complex if the code uses ranged array, like this:
a = b[1:10]
Since there is no relevant grammar in C++, we have to do some fundamental support in C++. Firstly, there should be an object which could represent a range of an array. Say, we have this:
class ArrayDimDesc
{
int size;
int startIndex;
int endIndex;
};
class RangedArray
{
void* array;
int Dimensions;
ArrayDimDesc arrayDimDesc[8];
};
So IDL ranged array b[1:10] could be represented by class RangedArray object. This way made things easier. Replace b[1:10] by a function call to MakeArrayRange1D(), the whole statement can be translated just as simple array subscript case.
Let me show you a more complex IDL code:
coeffbk = where(sset.bkmask[nord:*, 1, 2:3] NE 0)
And the translated C++ code is:
coeffbk = where(MakeArrayRange3D(sset.bkmask, nord, -2, 1, -1, 2, 3) != 0);
I haven't finished all the work. But the main idea is just as above. There are lots of further work to do:
1, Memory management
2, Introduce smart pointer?
3, Is it necessary to move MakeArrayRange3D in front of the statement?
==== BISON grammar rules for IDL ranged array ======================
postfix_expression:
primary_expression
| postfix_expression '[' array_subscripts ']'
{
$$ = AllocBuff();
// check if array_subscripts is a range
if(IsRangedSubscripts($3))
{
FillMinusOneToEmptyRange($3);
// case 1: range, compose a ranged array; $3 is subscripts
// MakeArrayRange(array, startIndex, endIndex)
if($3->dimension == 1)
{
// 1 dim
sprintf_s($$, TEXT_BUFFER_LEN, "MakeArrayRange1D(%s, %s, %s)",
$1,
$3->subscriptsRange[0].rangeStart,
$3->subscriptsRange[0].rangeEnd);
}
else if($3->dimension == 2)
{
sprintf_s($$, TEXT_BUFFER_LEN, "MakeArrayRange2D(%s, %s, %s, %s, %s)",
$1,
$3->subscriptsRange[0].rangeStart,
$3->subscriptsRange[0].rangeEnd,
$3->subscriptsRange[1].rangeStart,
$3->subscriptsRange[1].rangeEnd);
}
else if($3->dimension == 3)
{
sprintf_s($$, TEXT_BUFFER_LEN, "MakeArrayRange3D(%s, %s, %s, %s, %s, %s, %s)",
$1,
$3->subscriptsRange[0].rangeStart,
$3->subscriptsRange[0].rangeEnd,
$3->subscriptsRange[1].rangeStart,
$3->subscriptsRange[1].rangeEnd,
$3->subscriptsRange[2].rangeStart,
$3->subscriptsRange[2].rangeEnd);
}
else
{
// unsupport dimension
yyerror("Unsupported array dimension in ranged array, dimension is %d.\n", $3->dimension);
YYABORT;
}
}
else
{
// case 2: scalar, simple and easy case
char *subscript_buf = AllocBuff();
subscript_buf[0] = 0;
int pos = 0;
for(int i=0; i<$3->dimension; i++)
{
sprintf_s((subscript_buf+pos), TEXT_BUFFER_LEN, "[%s]", $3->subscriptsRange[i].rangeStart);
pos = strlen(subscript_buf);
}
// ##ATTENTION :IDL array subscripts are different than those of C++'s##
sprintf_s($$, TEXT_BUFFER_LEN, "%s%s",
$1,
subscript_buf);
// release subscript_buf
}
printf("array final code: %s\n", $$);
}
| postfix_expression '.' IDENTIFIER
{
$$ = AllocBuff();
sprintf_s($$, TEXT_BUFFER_LEN, "%s.%s", $1, $3);
}
| function_call
;
array_subscripts:
array_subscript
{
$$ = Malloc(sizeof(struct ArraySubscripts));
$$->dimension = 1;
strcpy($$->subscriptsRange[0].rangeStart, $1->rangeStart);
strcpy($$->subscriptsRange[0].rangeEnd, $1->rangeEnd);
// release $1
}
| array_subscripts ',' array_subscript
{
strcpy($$->subscriptsRange[$$->dimension].rangeStart, $3->rangeStart);
strcpy($$->subscriptsRange[$$->dimension].rangeEnd, $3->rangeEnd);
$$->dimension++;
}
;
array_subscript:
expression
{// single subscript
struct ArraySubscript *pArraySubscript = Malloc(sizeof(struct ArraySubscript));
strcpy(pArraySubscript->rangeStart, $1);
pArraySubscript->rangeEnd[0] = NULL;
$$ = pArraySubscript;
}
| range_or_whole_range ':' range_or_whole_range
{// Ranged subscript
struct ArraySubscript *pArraySubscript = Malloc(sizeof(struct ArraySubscript));
strcpy(pArraySubscript->rangeStart, $1);
strcpy(pArraySubscript->rangeEnd, $3);
$$ = pArraySubscript;
}
;
range_or_whole_range:
expression
| '*' { $$ = "*"; }
;
Wednesday, March 11, 2015
Optimizing by ANSYS - Objective Function Defined by Other Software
- You have an objective function to be optimized (to find a minimum/maximum point)
- But the function is defined by a software other than ANSYS
- The function doesn't have a mathematical formula
Suppose you have an objective function which is defined by a complex software, say an FEM or CFD software. The function has one or several variables, and the function generates different values regarding different variable values input.
Basic idea of the solution is by using /sys command in ANSYS APDL code which let ANSYS to invoke your software, and evaluate the function. ANSYS will invoke your software repeatedly until it finds a minimum/maximum point.
Key points for data transfering from ANSYS to your software and vice versa:
1, Your software that defining the objective function should parameterizing the model (the function), so that it can accept one or more parameters and generates the function value.
2, Your software should have a way of passing value to parameter(s), such as command line or an input file.
3, Your software should have a way of passing back the function value, such as program exit code or an output file.
Here is an example of the solution with the way of passing value that passes data by input/output file.
TASCA is the software which defines the objective function. "tascain.txt" is the input file, and tascout.txt is the output file.
vwrite puts the value of X to the input file. Then the code invokes TASCA. TASCA will read the data from "tascain.txt", and evaluate the function, and then writes the function value to "tascaout.txt". The function value will be read from the file and stores in A1 and V.
FILE: volu.inp
*DIM, A1, array, 2,1
X=1.4
*cfopen, tascain,txt
*vwrite, X
(F10.2)
*cfclose
/sys, TASCA evaluate
*vread, A1(1,1), tascaout,txt, c:\working, JIK, 1,1
(F6.2)
V=A1(1,1)
The following shows the optimization control file. V defined as the objective function. The makes ANSYS to find the minimum value of the objective function. To start optimization, type these command in the command input box of the ANSYS: /input, optovlu,inp .
FILE: optvolu.inp
/clear,nostart
/input,volu,inp
/opt
opanl,volu,inp
opvar,X,dv,1.3,1.9,1e-2
opvar,V,obj,,,1e-2
opkeep,on
optype,subp
opsave,optvolu,opt0
opexec
Friday, March 6, 2015
Loading Resources From Assembly and Assign to An Element Programatically
<Style x:Key="TextBlock_left" TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="5" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
And then you want to assign this style to an element:
DataGridTextColumn.ElementStyle = (Style)FindResource("TextBlock_right");
That's it!
Tuesday, January 6, 2015
Free Charting Tool For WPF C#
http://www.mesta-automation.com/real-time-line-charts-with-wpf-and-dynamic-data-display/
Tuesday, December 3, 2013
Simplify Asynchronous Programming with C# "yield return"
(Next article: Nested IEnumerable functions.)
OK, here we started with my understandable implementation.
Asynchronous programming is extremely important and useful when you are developing an internet server which is going to support large concurrent requests. When dealing with internet data transmission, blocking mode with one thread handling one request is the one of the most popular traditional technologies. Programmers like it because it's easy to implement. On par with traditional programming technologies, asynchronous programming has many advantages.
- Far less threads involved, reduce large amount of memory usage.
- Since you don't have to create so many threads, its really responsive to handle an incoming request.
So, how to make your code stay in beautiful, logical, human readable form of linear structure, avoiding of tearing it into pieces, but still you could enjoy the high performance asynchronous programming technology? The answer is "yield return".
The basic idea of yield return is to form a state machine by writing the state changing logic into a single function or nested function calls. The coding style looks like the code of traditional technology of blocking mode data transmission. Every yield return will return from the function with all state reserved, and when last time the function being called, the code will be executed from last yield return statement.
See this simple example:
private IEnumerable<int> Foo()
{
// some code here
yield return 1;
// some code here
yield return 2;
// some code here
yield return 0;
// some code here
}
Foo() is a function, also it's a state machine. Every time you call it the function will resume execution at the point that last time returned. For sure that every local variable remains in its state of last yield return. OK, let's see how to execute this function:
private void Driver()
{
foreach (var ele in Foo())
{
// do something
}
}
It's time to show the core of the technology. Let's incorporate the state machine function with asynchronous function calls.
private IEnumerable<int> Foo()
{
var ar = HttpWebRequest.BeginGetResponse(callbackHandler);
yield return 1;
var response = HttpWebRequest.EndGetResponse();
response.DoSomthing();
}
The code isn't real working code, just for theory illustration. The first statement calls BeginGetResponse() passing a callback handler in. The callback handler won't be called until BeginGetResponse() finishing the work. During this period of time, no thread of your app work for these stuffs. So you don't have to pay the price to wait for BeginGetResponse() to return, for example, a thread waiting by semaphore or event. Thus we have a chance to make Foo() to execute the rest of the code. But Driver() can't drive the execution for us. So we have to upgrade our Driver() to a real driver which can drive Foo() through all of its async steps.
public class AsyncDriver
{
public IEnumerator<int> iterator;
public void AsyncCallback(IAsyncResult ar)
{
DriveToNext();
}
private void DriveToNext()
{
iterator.MoveNext();
}
public void AsyncCallback(IAsyncResult ar)
{
DriveToNext();
}
}
public void Main()
{
AsyncDriver asyncDriver = new AsyncDriver();
asyncDriver.iterator = Foo().GetEnumerator();
}
private IEnumerable<int> Foo(AsyncDriver asyncDriver)
{
var ar = HttpWebRequest.BeginGetResponse(asyncDriver.AsyncCallback);
yield return 1;
var response = HttpWebRequest.EndGetResponse();
response.DoSomthing();
}
So we have AsyncDriver class. Pay attention to BeginGetResponse(), as you can see the call back function is offered by AsyncDriver. When BeginGetResponse() finished its work, AsyncDriver will take control, and it drive the async steps in Foo() by calling iterator.MoveNext(). The 'iterator' is from Foo().
That explains the theory of driving async steps of an IEnumerable function. Real code would be more complex.
Here is the complete code of AsyncDriver. And follow that is the usage of AsyncDriver.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Common
{
public class AsyncDriver
{
private class AsyncDriverResult : IAsyncResult
{
private object asyncState;
public object AsyncState
{
get { return asyncState; }
set { asyncState = value; }
}
public WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}
public bool CompletedSynchronously
{
get { throw new NotImplementedException(); }
}
private bool isCompleted;
public bool IsCompleted
{
get { return isCompleted; }
set { isCompleted = value; }
}
}
private AutoResetEvent executionFinishedEvent;
private Mutex mutex;
private AsyncDriverResult asyncDriverResult;
private AsyncCallback AsyncDriverCallback;
private Exception terminationException;
private IEnumerable<int> enumerable;
private IEnumerator<int> iterator;
public delegate IEnumerable<int> AsyncEnumerator(AsyncDriver asyncDriver, object state);
public AsyncDriver()
{
mutex = new Mutex();
}
public void AsyncCallback(IAsyncResult ar)
{
DriveToNext();
}
private void DriveToNext()
{
ThreadPool.QueueUserWorkItem((aeObject) =>
{
mutex.WaitOne();
try
{
bool tonext = iterator.MoveNext();
if (tonext)
{
if (iterator.Current == 0)
{
OnExecutionFinished();
}
}
else
{
OnExecutionFinished();
}
}
catch (Exception ex)
{
terminationException = ex;
ex.Data.Add("PtolemaicCallStack", ex.StackTrace);
OnExecutionFinished();
}
finally
{
mutex.ReleaseMutex();
}
},
this);
}
private void OnExecutionFinished()
{
iterator.Dispose();
executionFinishedEvent.Set();
AsyncDriverCallback(asyncDriverResult);
}
public IAsyncResult BeginExecute(AsyncEnumerator asyncEnumerator, AsyncCallback callback, object state)
{
asyncDriverResult = new AsyncDriverResult();
asyncDriverResult.AsyncState = state;
enumerable = asyncEnumerator(this, state);
AsyncDriverCallback = callback;
Execute();
return asyncDriverResult;
}
public void EndExecute(IAsyncResult ar)
{
executionFinishedEvent.WaitOne();
if (terminationException != null)
{
throw terminationException;
}
}
public void Execute()
{
executionFinishedEvent = new AutoResetEvent(false);
iterator = enumerable.GetEnumerator();
DriveToNext();
}
public static string DumpException(Exception ex)
{
string msg = "\n=================================================================\n"
+ ex.ToString() + "\n"
+ ex.Message + "\n\n" + ex.StackTrace + "\n";
if (ex.Data.Contains("PtolemaicCallStack"))
{
msg += "\n---- PtolemaicCallStack ----\n" + ex.Data["PtolemaicCallStack"];
}
msg += "\n=================================================================\n";
return msg;
}
}
}
The usage of AsyncDriver:
public void Main()
{
Common.AsyncDriver asyncDriver = new Common.AsyncDriver();
var serverContext = new ServerContext();
serverContext.AsyncDriver = asyncDriver;
asyncDriver.BeginExecute(Foo, FooCallback, asyncDriver);
}
private void FooCallback(IAsyncResult ar)
{
var asyncDriver = ar.AsyncState as Common.AsyncDriver;
asyncDriver.EndExecute(ar);
}
private IEnumerable<int> Foo(Common.AsyncDriver asyncDriver, object state)
{
var ar = HttpWebRequest.BeginGetResponse(asyncDriver.AsyncCallback);
yield return 1;
var response = HttpWebRequest.EndGetResponse();
response.DoSomthing();
}
Friday, October 18, 2013
[Windows Service Debugging] Separate code from service app for convenient debugging
- Create a windows service project that only has VS generated code in it.
- Create a DLL proeject that contains the service object.
- Let service project call the service object to start it.Then your service would work. But this is not for debugging.
- Create a normal windows application, and just as the service project, just call the service object ans start it. This application is convenient for you to debug the service object.
Wednesday, September 25, 2013
(Visual Studio) Per configuration application icon setting
On Visual Studio 2010 UI, you can only the one icon for all configurations. But when you open .csproj file as plain text, you can see it's real powerful. All per configuration settings are under tag like this:
So just find
Monday, November 12, 2012
HTTPS Communication – HttpListener based Hosting and Client Certification
(Just found, a wonderful tool set that could help you to host easily, http://katanaproject.codeplex.com/. The site referred my post, and I didn't realize till now :-) thanks for refering. [2016-2-6])
HttpListener is the easiest way for you to host an HTTP/HTTPS server. This article provides you step-by-step instructions to create your own server and authenticate clients based on client certificate from ground up in C#.
Download the sample code
STEP 1
Firstly, you should create your .net application and add these four lines.var server = new HttpListener();
server.Prefixes.Add("https://+:90/");
server.Start();
HttpListenerContext context = server.GetContext();
These fourlines will make your server started and listening on the port. Be aware of the exceptions (HttpListenerException) thrown from the invocation server.Start(), and see step 2 to solve it.
STEP 2
Step 1 shows you it’s so easy to start a server. But wait, Start() throws an exception (HttpListenerException: Access Denied, native error code 5, HRESULT 80004005), if you run your app under non-privilege account. If you want a non-privilege account to run the server, you have to add ACL (Access Control Lists) to the system. In command line:netsh http add urlacl url=https://+:80/MyUri user=DOMAIN\user
Pay attention to the parameter ‘user’. Put whatever user you want to assign the start server right to here. If set the parameter user=users, it will grant all user account (non-privileged) to start the app and listen on the specific ip and port. The ip part ‘+’ stands for all IPs of your machine. For the server you want to handle urls from root (e.g. http://localhost/), you don’t need ‘MyUri’ part, and your command is like this:
netsh http add urlacl url=https://+:80/ user=DOMAIN\user
STEP 3
And then your app won’t throw any exception. Your app would be blocked at server.GetContext() and waiting for incoming connections. Try the url https://localhost:90/ in your browser, there is still an error page with HTTP 101 ERR_CONNECTION_RESET. This because you haven’t assign a certificate to the server and the browser can’t verify the validity of the server. Remember we are visiting an HTTPS site. The server certificate is a must.So, let’s create the certificates. You can either create your certificates by makecert or by OpenSSL. And this How to Setup a CA gives you an easy tutorial of creating certificates hierachy by OpenSSL. First is the root CA certificate. For experimental cases, makecert is enough. But for product, you may want to use OpenSSL or apply a certificate from CA like VeriSign.
makecert -n "CN=TestCA" -r -sv TestCA.pvk TestCA.cer
And import the root certificate to the system certificate storage of Rusted Root Certification Authority. See this article.
Then create the certificate for your HTTPS web site.
makecert -iv TestCA.pvk -n "CN=TestSite" -sv TestSite.pvk -ic TestCA.cer TestSite.cer -sr LocalMachine -ss My -sky exchange -pe
If you will test your client app on a machine other than the server machine, you have to import the TestCA.cer to the client machine as well. So that the client machine trust TestCA (the root cert), it will also trust the server certificate (TestSite).
Hosting an HTTPS site, you must have a certificate with private key. But the last makecert command creates the private key in TestCA.pvk which can’t be imported to the system storage directly. We have to convert it to .pfx format:
pvk2pfx -pvk "TestSite.pvk" -spc "TestSite.cer" -pfx "TestSite.pfx"
Then you will see the certificate for your site:
STEP 4
How to use the server certificate? At this point, the when client connect to the server, the client will throw an exception (WebException The underlying connection was closed: An unexpected error occurred on a send), simply because the server doesn’t use the certificate yet. To resolve the exception,just binding the certifiate to the server’s ip and port by netsh.netsh http add sslcert ipport=0.0.0.0:90 appid={61047666-992C-4137-9303-7C01781B054E} certhash=75d0fed71881f2141b5b6cb24801dfa554439b1c clientcertnegotiation=enable
‘0.0.0.0’ in the ipport parameter means every ip of this machine would be assigned with the certificate. The parameter appid is your application id. You can see it in the project property, the ‘Application’ page, and the dialog poped up by clicking ‘Assembly Information’ button. The parameter ‘clientcertnegotiation=enable’ will allows C/S mutually authentication based on certificates, i.e. server side could verfiy the certificate validation of the client side as well as the client side verifying the server side. If you don’t want verification for client side, just omit the parameter.
STEP 5
Visit https://localhost:90/ again, your browser will warning you that the site is not the owner of the certificate. It’s because we don’t have a domain for our experimental site and no domain name was set into the certificate. So just click continue to view the page and the browser will show you a blank page.Let’s add responding code to the server side, so that we can see something on the page.
string message = "Hello World!";
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
Now the page displays “Hello World!”.
STEP 6
We have done the work of constructing server side. The server can show its identity by providing its certificate and client can verify it. Client still shows no certificate to the server. In some cases, the server need to verify the client’s identity, and only when the client is valid (e.g. a valid member of some organization) the server would start data communication. In this case, a client app (other than web browser) is a must. So let’s create a client app.Here is the basic client code without client certificate.
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CheckValidationResult);
string url = "https://localhost:90/";
Console.WriteLine("Visiting " + url);
HttpWebRequest objRequest = System.Net.HttpWebRequest.Create(url) as HttpWebRequest;
objRequest.ProtocolVersion = HttpVersion.Version10;
var response = objRequest.GetResponse();
var responseReader = new StreamReader(response.GetResponseStream());
var responseContent = responseReader.ReadToEnd();
Console.WriteLine("Server replied: " + responseContent);
CheckValidationResult is a callback function which allows you to perform customized validation against server certificate, returns true to accept the certificate. As expected, the client gets the server reply: “Hello World!”.
STEP 7
Here we add client certification code. Basically you have two ways of creating a X509Certificate2 which could contain public/private key pair. Other ways like manipulating public/private key pair raw data directly, may be tricky and complex.- Load .pfx from file;
- Load certificate with private key from the system’s certificate store.
HttpWebRequest objRequest = System.Net.HttpWebRequest.Create(url) as HttpWebRequest;
X509Certificate2 clientCertificate = new X509Certificate2("TestClient.pfx", "the key password");
objRequest.ClientCertificates.Add(clientCertificate);
You have to add certificate to the https request right after you created the request, because GetResponse() will use the certificate immediately. Here is the second way of creating X509Certificate2 - loading the certificate from the system store:
static X509Certificate2 LoadClientCertificate()
{
// Note: Change "My" and StoreLocation.CurrentUser to where your certificate stored.
var store = new X509Store("My", StoreLocation.CurrentUser);
var certificates = store.Certificates.Find(X509FindType.FindBySubjectName, "TestClient", true);
if (certificates.Count != 0)
{
return null;
}
return certificates[0];
}
Before running it, you have to import the certificate (with private key) to the store just like you did with the server certificate. Loading a certificate (without private key) can be done by a non-privileged account, while accessing private key of a certificate from the system store requires administrator privilege. So when you run above code by a non-privileged account, you will get the certificate although, but only public key is in it. While the server side needs the client to sign something to verify the client’s identity, so the client must have the private key. So when carrying out further steps of HTTPS communication
- When the client certificate loaded from system store, the client code will get an WebException;
- When the client cerfiticate loaded from file, the server will get no client cert (GetClientCertificate() returns null).
Loading from store and loading from file both has pros and cons.
STEP 8
Server side still doesn’t verify the client certificate. So let’s add the code logic.HttpListenerContext context = server.GetContext();
var clientCertificate = context.Request.GetClientCertificate();
X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.Build(clientCertificate);
if (chain.ChainStatus.Length != 0)
{
// Invalid certificate
context.Response.OutputStream.Close();
}
X509Chain is a tool which builds the chain of trust of the certificate. If the certificate is invalid, then you can find error information in chain.ChainStatus. You can implement detailed logic upon X509Chain rather than only checking chain.ChainStatus.Length. Set RevocationMode to NoCheck because we don’t have a certificate server to tell you whether a certificate is revoked.
Wednesday, August 22, 2012
Render English and Chinese with mono font
How to make a Chinese character exactly 2 English characters wide?
I've tried it in WPF application with Courier New and Segoe UI Mono, but both failed. And also tried SimSun as fallback font, failed again.
Here is the summary of my test.
Successful Examples
Notepad
Notepad++
HTML with pre tag rendered by Chrome
Failed Examples
Visual Studio 2010
HTML without pre tag rendered by Chrome
HTML with and without pre tag rendered by IE
I tried Courier New and Segoe UI Mono with fallback font SimSun in my WPF app. The fallback font mechanism worked, because it can render STLiti which is 隶书, see below
One Chinese char is a little bit less than 2 English chars in width. when fallback font is SimSun, it's the same case
Visual Studio has the same issue
While Notepad++ is pretty successful
Different web browser has different behavior. With
<pre> tag would work well in Chrome while it can't give you Chinese char of 2 English chars width without
<pre>. And IE can't do it in both cases.
As you can see Chinese chars in Figure 3 is a little bit wider than Chinese chars in Figure 1. Is there a covered mechanism results narrow Chinese chars?
Tuesday, March 6, 2012
Config Test Deployment for VS2010
Sunday, June 12, 2011
Mean-shift Object Tracking Study Notes
Xueqing Sun
June 12th, 2011
You can distribute, remix, tweak, and build upon this work, even commercially, as long as you credit Xueqing Sun for the original creation.
I was reading a book, ‘Image Processing, Analysis, and Machine Vision, Milan Sonka, Vaclav Hlavac, Roger Boyle’ for studying mean-shift object tracking algorithm. But the book was really hard to understand, especially for a beginner of machine vision. Yesterday, with the help of Guangwei, a friend of mine (he is a scientist at National Astronomical Observatories, Chinese Academy of Science), I finally understood the algorithm. And I would like to explain what I understood for sake of helping other guys who are still struggling on this algorithm.
Heads up Questions
1. What is the probability density function of an image?
2. How does the density function contribute to target model?
3. What’s kernel function? How does it work?
Here is the summary of the algorithm
To locate a target on an image, we must have a target model, an image which contains the target. The goal of mean-shift algorithm is to tell you the position of your target on the image. Suppose we have a target model image with 5*5 pixels. The pixel values listed below:
| x | y | pixel |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 2 | 6 |
| 0 | 3 | 5 |
| 0 | 4 | 7 |
| 1 | 0 | 0 |
| 1 | 1 | 4 |
| 1 | 2 | 7 |
| 1 | 3 | 0 |
| 1 | 4 | 4 |
| 2 | 0 | 3 |
| 2 | 1 | 5 |
| 2 | 2 | 7 |
| 2 | 3 | 1 |
| 2 | 4 | 5 |
| 3 | 0 | 7 |
| 3 | 1 | 0 |
| 3 | 2 | 9 |
| 3 | 3 | 9 |
| 3 | 4 | 2 |
| 4 | 0 | 0 |
| 4 | 1 | 1 |
| 4 | 2 | 4 |
| 4 | 3 | 3 |
| 4 | 4 | 3 |
Target model image
The row x and y are the location of a pixel on the image. And the third row ‘pixel’ is the value. For convenient purpose, we limit pixel value from 0 to 9. Now we can create a frequency table like below. Normally images are colored, but we simplified the problem from colored images to gray scale images. A colored image can be considered as a composition of three gray scale images. So the rationale explained here can be easily applied to colored images.
| bin | q |
| 1 | 8 |
| 2 | 1 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 1 |
| 7 | 4 |
| 8 | 0 |
| 9 | 2 |
Frequency
The frequency means how many pixels located in a specific bin. For example, the first row means there are 8 pixels which values are less or equal to ‘1’. Furthermore we can have histogram chart for the table.
Histogram
On the book, and many other materials, the density function of target model, defined as below:
| 1 |
The density function is closely related with the frequency table. Here the black
is a vector which has m components. In this case, the m is 9 because we have 9 bins. Each component is made from corresponding bin. Let look into the component definition.
| 2 |
The definition looks somewhat complex. Let’s split it into parts so we can understand it easier. First, let’s see the delta part
| 3 |
n is the pixel number. In this case we have 25 pixels. So the n is 25.
is the coordinate of ith pixel in the image. For example, the first pixel coordinate is (0,0), and the second is (0,1). u means we are currently defining uth component corresponding to uth bin. Function
tells which bin the pixel
go. For example, the first pixel (0,0) is 1, so it goes to bin 1, i.e.
.
Actually, the formula 3 is a vector, let’s rewrite it as following:
| 4 |
The delta function gives you 1 if the pixel
goes to bin u, otherwise it gives 0. So this vector works just like a mask. On the mask there are ‘1’s mark the pixels go to bin u. For example, if our image is 4*4, we might get a D vector like this:
| 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 |
The image is a matrix. D is a vector. Just put each row of the matrix to one row in sequence then we’ll get D.
IMPORTANT
What’s the relationship between D and the frequency table? The answer is for each u,
. Actually we can form feature space only based on D, because D already represents the image in statistics way. So mean-shift algorithm is so called an algorithm that based on probability density function. But marginal pixels are unstable, so the kernel function which is a weight function does the work. It give marginal pixels less weight to make the objective function in optimization process more stable.
OK, let’s see another part of formula 2. It’s
| 5 |
Again it’s a vector. Function k is the kernel function which actually is a weight function giving each pixel position a weight. So
can be defined as below:
| 6 |
It looks beautiful. I really hate
which makes formula unreadable.
OK, now we get the density function of target model image. The candidate density function is pretty similar.
| 7 | |
| 8 |
Formula 8 is easier to understand because we’ve already understood formula 2. The k function in formula 8 is just a transformed version of original k.
I think I’ve solved the hardest part of the mean-shift algorithm. And the rest of it would be much easier to understand because it is just kind of an optimism algorithm to find the y.





