# Smith Chart Using MATLAB

The Smith Chart is the graphical tool for solving the problems with transmission line in RF engineering. The Smith chart can be used to display different parameters like Impedance, Admittance, Reflection coefficient, Noise figure Circles, Stability etc.

The Smith Chart is plotted on complex reflection coefficient plane in two dimension. It is scaled in normalized impedance/ admittance. Smith chart can be divided in sub categories like Z smith chart, Y smith chart, ZY smith chart.
The following code plots the smith chart for you.

### MATLAB Code:

clear all;
close all;
clc;

%% Real Circles
for r = 0:0.2:10                                                                  % Decide the no of real circles
a= (1/((1+r)));
b=0;
c=(r/(1+r));
a0= (1/((1.*(sin(0.5.*(pi))))));                                          % for r=0;
b0= cos((0.5*(pi)))./(1.*(sin(0.5*(pi))));                          % for r=0
x= c-a:0.00002:c+a;
y1= sqrt(a.^2 -(x-c).^2)-b;
plot(real(x),real(y1),'Color',[0,0,0]);
hold on;
y2= -sqrt(a.^2 -(x-c).^2)-b;
plot(real(x),real(y2),'Color',[0,0,0]);
hold on;
y3= sqrt(a.^2 -(x-c).^2)+b;
plot(real(x),real(y3),'Color',[0,0,0]);
hold on;
y4= -sqrt(a.^2 -(x-c).^2)+b;
plot(real(x),real(y4),'Color',[0,0,0]);
hold on;
end
hold on;

%% Imaginary circles
for q= 0.1:0.2:10                                                           % Decides the no of imaginary circles
e= (1/(q));
d= (1/(q));
z=1-d : 0.00002: 1+d;
y5= sqrt(d.^2- (z-1).^2)+e;
err=z.^2+(y5+b0).^2-a0.^2;           %error function so that imaginary circle remains inside the real circles
idx=find(err<=0);
plot(real(z(idx)),real(y5(idx)),'Color',[0,0,0]);
hold on;
y6= -(sqrt(d.^2- (z-1).^2))+e;
err=z.^2+(y6+b0).^2-a0.^2;
idx2=find(err<=0);
plot(real(z(idx2)),real(y6(idx2)),'Color',[0,0,0]);
hold on;
y7= sqrt(d.^2- (z-1).^2)-e;
err=z.^2+(y7-b0).^2-a0.^2;
idx3=find(err<=0);
plot(real(z(idx3)),real(y7(idx3)),'Color',[0,0,0])
hold on;
y8= -(sqrt(d.^2- (z-1).^2))-e;
err=z.^2+(y8-b0).^2-a0.^2;
idx1=find(err<=0);
plot(real(z(idx1)),real(y8(idx1)),'Color',[0,0,0]);
hold on;
title('Smith Chart');
xlabel('Real');
ylabel('Imag');
end

%% Central Line having infinite radius

hold on;
y=0;
x=-sqrt(a0.^2-b0.^2): 0.001:sqrt(a0.^2-b0.^2);
plot(x,y,'Color',[0,0,0]);                                                             % Draw the central straight line

You can also download this code by your mathworks account using this link : Smith-chart

For more questions about the code/implementation you can comment or contact the author mentioned below.

Smith Chart

# Writing a MATLAB virus.... (Please save all your work before Trying it!)

Just recently, I came in front of this method. Which will literally blow you mind (And your PC's RAM  too). MATLAB is a resource heavy software, requires a lot of CPU computation power and RAM.
Imagine, what will happen if 10 to 20 or even 100s of instances of MATLAB will be open at once in your computer system! It will call a disaster, possibly your system will crash (When I tried, I needed to Shut Down my system through the power button.)

How to create a simple MATLAB virus? Steps:

(1). Write in a new script file the following line:
! matlab.exe &

(2).  Save the file with the name "startup.m", in the folder "%userprofile%\documents\MATLAB\"

Here  %userprofile% is your user name in the computer system, which you are using.

DONE?

 MATLAB Virus

Now Press the Run button!

Within a second you will see is something like this....... (If you are able to open task manager).

 Writing a MATLAB virus.... Multiple instances of MATLAB at once!

After this screenshot, my computer stopped responding and I need to Shut it Down  through power button.

What just happened?
As the  first MATLAB instance calls another instance through the matlab.exe line code,  the new instance executed the startup.m again, calling in recursion again and again a new instance of MATLAB.

DISCLAIMER:  I don't intent any harm of your work/computer/anything, you will have to run  this code at your risk. But please save your work before doing this.

How to undo it for next time?
Simply delete the startup.m file from %userprofile%\documents\MATLAB\ directory before you start again the MATLAB.

# Using MATLAB as an Alternative Web Browser

Yes, you read it right! MATLAB as a Web Browser.
You can simply browse the internet over it, see the below examples.....

Opening  www.facebook.com in MATLAB.
 Opening www.facebook.com in MATLAB

Opening  www.mathworks.com in MATLAB.
 Opening  www.mathworks.com in MATLAB.
So How to Browse?

Type the following commands in Command Value or in a Script.

>> address='http://www.mathworks.com'
web(address)

or simply
web ('https://www.facebook.com');

Is an append of "http://"  or "https://" is necessary?
No. Try even without them....it will work!
web ('www.facebook.com');

# Creating Karaoke of Music/Sound Using MATLAB

We all are familiar with Karaoke. And if you own a computer system, in past at-least once you might have tried to create a Karaoke from it. You might have googled software for it. Some of which you have found, are open-source and some, shareware with limitations.

But if you are a MATLAB coder why you want to do it all. Simply, do it MATLAB.

Just a few concepts you need to know for creating Karaoke from normal music file and you are all set to code.

For your information, There is no way to 100% remove the vocals from a song (Until and unless you are a DSP Engineer with passion to work with eerie details of finding the vocal frequencies and cutting them off). Accompaniment tracks are made in the studio, and our often referred to as minus one tracks because they are missing one track (in this case, the vocal track). The only way you would be able to remove the vocal track from the song entirely is if you have a multitrack version of the song, which is unlikely.

I am going to tell you a similar method:

Here I have used a stereo Wave file (.wav) to extract the instrument sound, separating it from the audio.

Following MATLAB code does the same.

[file, path] = uigetfile('*.wav','Select a .wav file');
if file == 0
return
end

out_dir = uigetdir(cd,'Choose output folder');
if out_dir == 0
return;
end

[y,Fs,nbits]= wavread(file);

if size(y,2) == 1
msgbox('The selected file is Mono. This algorithm is applicable only for Stereo files.');
return;
end

fc=input('Enter Cutoff Frequency (HPF):');
fc=round(fc);
if fc > 20
fp = fc+5;
fs = fc/(Fs/2);
fp = fp/(Fs/2);
[n wn] = buttord(fp,fs,0.5,80);
[b, a] = butter(5,wn,'High');
channel_2 = filtfilt(b,a,y(:,2));
else
channel_2 = y(:,2);
end

karaoke_wav = y(:,1) - channel_2;

%Write it to a file
[p name ext] = fileparts(file);

if isdir(out_dir)
wavwrite(karaoke_wav,Fs,nbits,[out_dir '\' name ext]);
else
wavwrite(karaoke_wav,Fs,nbits,[cd '\' name ext]);
end

%Apply and Enjoy.

NOTE:
This code will first convert your stereo sound (.wav) file to mono file. Then try to apply a High Pass Filter on the resultant file.

# Generate PDF, HTML, DOC file of your MATLAB code, figures, using MATLAB itself

MATLAB save to PDF, HTML, DOC.
Many times (almost every times, actually!) we use external methods or softwares to get our MATLAB code be converted to publish ready formats like, PDF, HTML or DOC. But MATLAB developers were smart enough, to provide some options so that a developer can export/publish his code into another format, which may be printer or web -friendly.
That option in MATLAB is known by the name "publish" command.

By default you can use the command like this:
publish('hello.m'); %Here hello.m is the matlab file you want to publish

It will generate a HTML file inside a folder named "html" in the current working directory. The HTML file will have the same name as your MATLAB (.m) file is.

But there are many other formats too that MATLAB supports.
Namely, PDF, DOC, XML, PPT, TEX & HTML being the default one.

For creating a PDF file for your MATLAB code, use the command:

publish('hello.m', 'pdf');

Similarly,
For DOC File,
publish('hello.m', 'doc');

For PPT FIle.
publish('hello.m', 'ppt')

Suppose in a scenario, you require to generate the report through a script in which you just want to get the location of the report with the file name, you need to just put a variable before giving the publish command.
x=publish('hello.m','html');
Now the variable 'x' will be a string variable, which contains value like this:
x =
C:\XYZ\html\hello.html

If you want to control other parameters of the documents like, max height, width, max no. of lines etc. You can define a structure variable and pass on to the parameters of publish command with the file name.

`op= struct('format','doc','outputDir','C:\XYZ', ...., ...., ...., 'Name', Value')`
`See help for a detailed view of options.`
`And pass that like:`
`publish('hello.m',op);`
```
```
```
```
`NOTE:`
`If you are using Academic version of MATLAB, then there would be a watermark in your published file. `

# Importing Excel Workbook/WorkSheet (XLS files) in MATLAB

At times we need to import Excel files in MATLAB, as they might be our data source. The data structure of XLS files are much like MATLAB itself, so MATLAB has got functionality for a direct import the content of these files as a variable.

In this example we have created a file named matlab.xls, which have got the following content, it contains data in sheet one only (Although these methods will be applicable for multiple sheets even).

 1 6 7 7 6 5 2 6 6 3 4 4 3 5 6 4 5 5 4 5 6 3 3 4 1 2 3 4 5 6 9 8 7 1 5 3

A 6x6 matrix.

Now we will look at how to import this in MATLAB.

1. Drag and Drop in MATLAB's Workspace
Just drag and drop your Excel (XLS) file in MATLAB's workspace, and you will see an import window appearing in front of you. There you will get many options, that will let you choose how to import this data.

 Importing XLS (Excel) file in MATLAB
2. Through IMPORT DATA button in newer MATLAB versions
Click on Import Data button, you will get a pop up window to browse your file to get imported.
 Importing XLS (Excel) file in MATLAB
3. Through uiimport command

Us the command like : uiimport('C:\Users\x\Desktop\matlab.xls')
You will get a window popup so that you can select the data which you want to import..

4. Using xlsread command
Use the command like: >> xlsread('C:\Users\x\Desktop\matlab.xls', 'Sheet1');
or in general,
xlsread(filename, sheetname);

This function also let you to separate numeric data & string data using the syntax.[ndata,sdata]=xlsread(filename, sheetname);
The numeric data will be saved in the first variable ‘ndata’.
The strings will be saved in the second variable ‘sdata’.