# Week #43- MATLAB

Page 1 of 1

## 3 Replies - 12254 Views - Last Post: 20 May 2011 - 07:47 PM

### #1 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11758
• Posts: 44,178
• Joined: 27-December 08

# Week #43- MATLAB

Posted 07 December 2010 - 07:57 PM

This week's challenge is MATLAB.

MATLAB is a high-level language as well as graphical environment developed by MathWorks for technical computing. It focuses on numeric computation, algorithm development, data analysis, and data visualization. MATLAB has a wide variety of technical applications including image and signal analysis and processing, finance, and bioinformatics. MATLAB also posesses 3D graphics capabilities, and can be easily integrated with other programming lagnuages including Java, C/C++, FORTRAN, COM, and Microsoft Excel.

Getting Started

Resources

Is This A Good Question/Topic? 1

## Replies To: Week #43- MATLAB

### #2 bodom658

• Villiage Idiom

Reputation: 113
• Posts: 1,123
• Joined: 22-February 08

## Re: Week #43- MATLAB

Posted 07 December 2010 - 11:11 PM

I work with MATLAB every single day, and use it as my desktop calculator. I love it!
As a computer engineering major, I've got a huge interest in technical computing, and one thing i like doing is modeling electrical signal processing. So, my code snippet is of just that, specifically AM Modulation, which you can read more about here.

I'll do this in the form of a tutorial, so everyone can follow what I'm doing. I won't go over any of the concepts covered on the wiki page, but I'll be using some terms from it.

Demonstrating the use of AM modulation in MATLAB is useful because it allows for the exploration of the functions, and can help give you a clear understanding of 'if I change X, Y happens', which is how I learn. So without further ado, let's get set up.

The amMod function I wrote looks like this:
```function [ retval ] = amMod(signal,time,mu,freq,amplitude)
%AMMOD Summary of this function goes here
%   Detailed explanation goes here
carrier = amplitude*sin(2*pi*freq.*time);
retval = (mu + signal).*carrier;
end

```

Basically, it takes an input SIGNAL, a TIME vector for that signal, the Am coefficient MU, the carrier signal FREQuency, and the carrier signal AMPLITUDE.
It then creates the carrier signal and applies the AM formula, returning the new transmission signal.

So, how do we use it? Let's create a signal x and time vector t.
```>> t = 0:0.0001:1;
>> x = cos(2*pi*5*t); % 5 Hz cos wave
>> plot(x,y);

```

MU can be any number between 1 and 0, frequency should be at least 2* that of the message, and the amplitude can be any positive number.
So let's call this thing, and plot it.
```>> y = amMod(x,t,0.75,20,5);
>> plot(t,y);

```

Looks cool right? And if you look carefully, you can see that the upper maxima of the y signal form the shape of the x signal. you may be able to see it better like:
```>> plot(t,y,t,x);

```

So, how do we get the original signal? Well, we can't. But we can come close! Given that this signal will be demodulated using an envelope detector, which is basically a rectifier, low pass filter, and DC level remover (high pass filter, really...) we'll always end up with some chop. The higher you make the carrier frequency though, the better you can make your output signal look. So, let's try to emulate what the device does. Let's start by rectifying it using the following function:

```function [ retval ] = rectify( signal )
%RECTIFY Summary of this function goes here
%   Detailed explanation goes here
for i = 1:length(signal)
if signal(i) < 0
signal(i) = 0;
end
end

retval = signal;

```

so literally,
```>> z = rectify(y);

```

Then, we need to low pass filter this thing. We do this by only using the local maximums in the array. So:
```>> d = z(local_max(z)); % type help local_max for more info

```

Now, we need to get a new time array, but we want to keep it consistent with the old one, so we do basically the same thing.
```>> dt = t(local_max(z));

```

Now, to deal with this DC shift. The average value of a sine wave is zero. So, if we expect an output that is a sin or cos wave, we expect it's average to be zero, right? Let's get the average, and subtract it from every point in d.
```>> d = d - mean(d);

```

Now you can see your results by plotting it!
```>> plot(dt,d);
>> plot(dt,d,t,x); % comparing to our original!

```

I love MATLAB, and have a huge interest in Signal Processing and technical computing, so this week's challenge was right up my alley (may even post a second one!)

Happy Coding!
~Bodom

EDIT: Was trying to fix syntax highlighting... no support for MATLAB on here XP If someone can PM me details about how to write a script for it, I'll make one up =D

This post has been edited by bodom658: 07 December 2010 - 11:13 PM

### #3 wartech

Reputation: 10
• Posts: 203
• Joined: 16-October 06

## Re: Week #43- MATLAB

Posted 08 December 2010 - 11:37 AM

Thanks for the resource links. I will be taking CS 375 Intro to Numerical Computing next semester and we will be using MATLAB. Maybe I can learn it over the semester break.

### #4 November-06

• D.I.C Regular

Reputation: 48
• Posts: 451
• Joined: 04-January 11

## Re: Week #43- MATLAB

Posted 20 May 2011 - 07:47 PM

I have MATLAB 2008 R2 and Visual Studio 2008.

I want to get the input via Visual Studio either with VB.NET or C#.NET and then
pass the input as parameters to MATLAB to be evaluated.

After MATLAB performs computation, it will then return a result to Visual Studio.

I just do not know how to pass values from Visual Studio to MATLAB and then back to Visual Studio.

Does anyone know how to do this? Or maybe a link that has a tutorial on this?