ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Convolution in Matlab with Code Examples

Updated on May 9, 2016

Discrete, Continues and Circular convolutions can be performed within seconds in Matlab® provided that you get hold of the code involved and a few other basic things.

This tutorial aims to:

  • Demonstrate the necessary components of the code used to perform convolution in Matlab in a simplified manner.
  • Visualize the results by plotting graphs of convoluted functions.

This Tutorial is arranged in the following sequence:

  1. Continues convolution.
  2. Discrete convolution.
  3. Circular convolution.

Logic:

The simple concept behind your coding should be to:

1. Define two discrete or contineus functions.

2. Convolve them using the Matlab function 'conv()'

3. Plot the results using 'subplot()'.

Continues Convolution in Matlab

We will demonstrate this by using two continues functions:

  1. A Pulse Waveform. Denoted by the 'square()' function. This is the input x(t) to the system.
  2. Transient response of a capacitor. Denoted by equation of the form a*e1/RC*t. This could be considered the impulse response h(t) of the system.

Matlab Code for Convolution

 tint = 0;
 tfinal = 0.05;
 tstep = 0.0005;
 
 t = tint : tstep : tfinal;
 
 x = 4 * square( 500*t, 50 ); //Use a function of your choice here.
 
 subplot( 3, 1, 1);
 plot( t, x);
 
 h = 400 * exp( (-400*t) ); //Use a function of your choice.
 
 subplot( 3, 1, 2);
 plot( t, h);
 
 t2 = 2*tint : tstep : tfinal*2; //Convoluted function requires a wider    range to be plotted completely.
 y = conv( x, h) * tstep;
 
 subplot( 3, 1, 3);
 plot( t2, y);

Line 1-5: Define the range of values for the time axis.

Line 7: A square wave is initialized by using the Matlab function 'square()' it has an amplitude of 4, ω = 500 rad/s, and duty cycle of 50%.

Line 9: Subplot() partitions the output window to accommodate 3 plots on a single screen i.e It creates a table of 3 rows and 1 column(s) and then the last argument in subplot() selects 1st plot for further working.

Line 10: plots x(t).

Line 12-15: Impulse response of the system h(t) is defined. Subplot() selects 2nd graph and plots h(t) on it.

Line 18: A new range is defined for the resulting convoluted function.

Line 19-22: x(t) is finally convoluted with h(t) and then plotted over this new range. *tstep was added to adjust the x and y axis accordingly.

Convolution in Matlab. (continues)
Convolution in Matlab. (continues)

Discrete Convolution in Matlab

Performing discrete convolution in matlab is very simple and straightforward. You just have to define the discrete values of each function and then apply the function 'conv()' on both of these functions.

Matlab Code for Discrete Convolution

 a = -1; //This is the starting value of time for x(t).
 b = 0;  //Starting value of time for h(t).
 
 x = [ 2 -1 1];
 h = [ 3 2 1];
 
 subplot( 3, 1, 1);
 t = a : a+length(x)-1; //tstep is not required here.
 stem( t, x);
 
 subplot( 3, 1, 2);
 t = b : b+length(h)-1;
 stem( t, h);
 
 y = conv( x, h);
 
 subplot( 3, 1, 3);
 t = a+b : a+b+length(y)-1;
 stem( t, y);

Line 4-5: Define discrete values for x(t) and h(t).

Line 8, 12 & 18: Define the range for the time axis in the graphs to be plotted subsequently using stem() function (if plotting is not required you may omit these steps).

Discrete Convolution in Matlab
Discrete Convolution in Matlab

Circular Convolution in Matlab

Circular convolution can be performed in the following steps:

  1. Take Discrete Fourier transform of two vectors.
  2. Multiply the two transforms.
  3. Take Inverse Discrete Fourier transform of the product and the result is the circular convolution of two vectors.

Matlab Code for Circular Convolution

// Define two vectors for circular convolution

x = [2 5 4 1];
y = [1 4 3]; 

// Zeropad vectrs uptill 4+3-1

xpad = [x zeros( 1, 6-length(x) )];
ypad = [y zeros( 1, 6-length(y) )];

// Multiply ffts of both vectors and take idft of product

ccirc = ifft( fft( xpad ).*fft( ypad ) );

// Now plot result

stem( ccirc, 'filled' )
ylim( [0 35] )
title('Circular Convolution of xpad and ypad');
Circular Convolution in Matlab
Circular Convolution in Matlab

© 2015 StormsHalted

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)