Archive

Archive for the ‘Uncategorized’ Category

RMS Formula ( Root Mean Square Formula)

March 2nd, 2012

The rms value of a set of n values for (x1, x2, …..xn) is given by

This formula is valid for a set of n discrete values. If you have a continuous variable, you can use Calculus to derive the formula for various common waveforms, for example the sinusoidal waveform.

For example if a is the amplitude of a sinusoidal waveform the vrms is given by

You can use the following calculator to calculate the rms value , if you know the peak to peak value

rms to peak to peak calculator

Probably you cam here searching for the formula for the Root Mean Square or, rms for the short. Before we give out the formula and the explanation, let us find the need of a root mean square.

A company Xing Hua, makes nuts and bolts and supplies it to various companies in US and Europe. The company makes a strict record of all the quality procedure that it adapts.

The nuts and bolts had their dimensions measured with automatic tools after they are finished and recorded in a spread sheet. That is a great !!! Said the CEO when he visited the shop and looked into the quality procedure adopted by them. The QA engineer then calculated the mean of the internal diameter of the nuts and found that the average of the dimension of is very close to the required dimension. The required dimension was 2.050 mm while their average dimension was 2.051 mm. Pretty close.

A few months later the order that came from Japan for an automobile company was rejected. The company went bankrupt.

Explanation

Had the QA engineer measured the rms average in place or regular average, they could have caught the fault much earlier saving the company from bankruptcy.

You may have now understood the story now. Let us say we want to hit an average dia of 2.50 mm. If one piece has dia of 1.5 mm and second one has a dia of 3.5 mm, the average is still 2.50 mm, but both are useless.

So we define root mean square.

We take the difference. The differences are as follows

For 1.0 mm the difference is +1.0 mm
For 1.5 mm the difference in -1.0 mm

The average is 0
But the root mean square is sqrt ( (1)^2 + (-1)^2) /2) = 1.0

This is 1.0 mm – non trivial value.

So coming back to the rms formula. Here is the formula for the RMS

The rms value of a set of n values for (x1, x2, …..xn) is given by

Other Resources

- If you are, however looking at the rms value corresponding to a waveform, you may like to check vrms to vpeak to peek calculator

Uncategorized

Photo Resizer program – Csharp Tutorial

February 17th, 2012

Basic Problem – Recent advent of high megapixel has created one problem – large image sizes. Today the Camera’s come at 15 Megapixels and higher. There are few issues with large size pictures

1. It takes longer to upload to facebook because of the size.
2. It is impractical to send large size image using email.
3. Viewing large size creates issues when viewing it in some photo viewer.

When you transfer the photos from your camera to your computer, it gets stored in a directory. We wish to write a simple program that will create a new directory and will put all the resized images in the new directory.

Here is goes.

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Tutorial
{
class ImageResize
{
enum Dimensions
{
Width,
Height
}

[STAThread]
static void Main(string[] args)
{
//set a working directory
string WorkingDirectory = @"C:\Users\abhinsv\Desktop\Imageresize_src";

string[] array1 = Directory.GetFiles(WorkingDirectory,"*.jpg");

Console.WriteLine("Enter the percentage you wish to compress");
int pc = Convert.ToInt32 (Console.ReadLine());

System.IO.Directory.CreateDirectory(WorkingDirectory + @"\images" + pc.ToString() + @"\");

foreach (string name in array1)

{

Image imgPhotoVert = Image.FromFile(name);
Image imgPhoto = null;

imgPhoto = ScaleByPercent(imgPhotoVert, pc);
imgPhoto.Save(WorkingDirectory + @"\images"+ pc.ToString()+ @"\" + Path.GetFileName(name), ImageFormat.Jpeg);
imgPhoto.Dispose();

}

}
static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent / 100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}

}

}

If you are uninitiated, you may like to take CSharp Tutorial.

Uncategorized

How to find list of files in a directory in Csharp – A tutorial

February 17th, 2012

In many cases you need to read the file names in a directory to be able to process or take some action to process the files. Here is a simple program that will list out the files in a directory.

using System;
using System.IO;

class Program
{
static void Main()
{
// Put all file names in root directory into array.
string WorkingDirectory = @"C:\Users\abhinsv\Desktop\Imageresize_src\";
string[] array1 = Directory.GetFiles(WorkingDirectory,"*.jpg");

Console.WriteLine("--- List of the Files: ---");
foreach (string name in array1)
{
Console.WriteLine(name);

}

Console.ReadLine();

}
}

Pay special attention to

Directory.GetFiles(WorkingDirectory,”*.jpg”);

It will list out all the files in the directory with the extension jpg. If you wish to list all files, irrespective of the extension, you may use

Directory.GetFiles(WorkingDirectory);

It will list all files with in the directory WITH path. Here is a typical output

--- List of the Files: ---
C:\Users\abhinsv\Desktop\Imageresize_src\imageresize_horiz.jpg
C:\Users\abhinsv\Desktop\Imageresize_src\imageresize_vert.jpg


If you want to list only the names of the file, you can use

Console.WriteLine(Path.GetFileName(name));

in place of

Console.WriteLine(name);

This will, for example give the following output

--- List of the Files: ---
imageresize_horiz.jpg
imageresize_vert.jpg

If you are uninitiated, you may like to take CSharp Tutorial.

Uncategorized

Create a new directory – Csharp Tutorial

February 17th, 2012

Creating a new directory is very simple using Csharp.  The following statement will create a new directory placed in the path C:\directory\subdirectory\. If the directory already exists with files in it, it will not be deleted and the files will not be removed.

System.IO.Directory.CreateDirectory("C:\directory\subdirectory\newdirectory);

using System;

namespace Tutorial
{
class CreateDirectory
{
static void Main(string[] args)
{
//set a working directory
string WorkingDirectory = @”C:\Users\abhinsv\Desktop\Imageresize_src”;
System.IO.Directory.CreateDirectory(WorkingDirectory + @”\images”);
}

}
}

If you are uninitiated in C Sharp, you may like to check CSharp Tutorial for beginners.

Here is the complete code to do this.

Uncategorized

PHP Tutorial – checkbox form

February 5th, 2012

Let us assume that you want to present a user with two or more than 2 choices and want him to take action based upon one or more selections he makes in the checkboxes.

To give you more exact example look at the following picture, where you are presented to choose one or more programming languages that you like.

Let us say you choose PHP and Javascript

If you hit the submit button the program should give the output something similar to

Here is the example code to do it.
<code>
<form action=”check_programs.php” method=”post”>
Which Programmming Languages do you like ? <br />
<input type=”checkbox” name=”chsarp” value=”No” /> Csharp <br />
<input type=”checkbox” name=”php” value=”Yes” /> PHP <br />
<input type=”checkbox” name=”javascript” value=”Yes” /> Javascript<br />
<input type=”submit” name=”checkform” value=”Submit” />
</form>

<?
if (isset($_POST['checkform']))

{
if  ($_POST['csharp'] == ‘Yes’)
{
echo “You like csharp”; echo “<br />”;
}

if  ($_POST['php'] == ‘Yes’)
{
echo “You like PHP”;echo “<br />”;
}

if  ($_POST['javascript'] == ‘Yes’)
{
echo “You like javascript”; echo “<br />”;
}

}
?>
</code>

You may like to check PHP Tutorial, if you are looking for getting started.

 

Uncategorized

Javascript Regular Expression – Greedy Vs Lazy – Quiz

February 4th, 2012

Take the following quiz for enhancing your understanding of the Regular Expression – Greedy Vs Lazy. You may like to ready the Javascript Regular Expression – Greedy Vs Lazy, before you take up this quiz.

Q1. Consider the following section of the code

var pattern = /^.*([\d]+)/;
var str = “Which year was known as great depression – 1929″;
var newstr = str.replace(pattern, “$1″);

what is the value of newstr

A. 1929
B. 9
C. Which year was known as great depression – 1929
D. 29

Q2. Consider the following section of code

var pattern = /^.*([\d]*)/;
var str = “Which year was known as great depression – 1929″;

What is captured in the parenthesis

A. 1929
B. 9
C. Nothing
D. Which year was known as great depression – 1929

Try to do the above two questions yourself before you check the answers. The answers are at the end of this topic. You may also like to check

1. Javascript Tutorial for beginners

2. Regular Expression In Javascript on amazon

Answers and explanations
Q1. B
We are tempted to think that the parenthesis will capture 1929. However the .* keeps
matching anything including the digits. Finally to make the match successful the last
digit 9 is left to match to [\d]+. This is captured by the parenthesis.
Q2. C
Note that the our greed to attempt to capture 1929 gives a blank result. All the literals
are captured in .* and for the search to be successful, no match is required

Uncategorized