How do I get the probability density function of a sine wave?

19 views (last 30 days)
Hi, I want to do something very simple in MATLAB which is just to get the probability density function of a sine wave and plot it. I know that the pdf plot has a U-shape, but I am not able to get it by using the pdf function in MATLAB no matter which "name" I use. For example:
x=-4*pi:.01:4*pi; y=sin(x);
So I just want to plot pdf for x between -1 and 1. Any help is appreciated. Thanks.
  3 Comments
Zach
Zach on 29 Aug 2012
Hi Wayne,
Thanks for your response. The random variable is just y (not x) between -1 and 1. So my plot should look like: http://imageshack.us/photo/my-images/692/sinepdf.jpg/. I would like to apply this to other signals, but I would first like to understand how to use the pdf function for this simple case. Thanks.
Star Strider
Star Strider on 30 Aug 2012
Edited: Star Strider on 30 Aug 2012
I'm not going to enter this as an ‘Answer’ because I can only take credit for Googling until I found a good discussion of it in EXAMPLES for Matlab Signal Processing (curiously without any associated MATLAB scripts). See ‘Probability Distribution of a Sine Wave’ pages 232-3, eqns 8.47-8.55 and figures 8.11 and 8.12.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 29 Aug 2012
Edited: Image Analyst on 29 Aug 2012
So why don't you generate a long sine wave signal and then pass it in to hist()? It's really really easy:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
omega = 1:80000;
period = 8000;
y = sin(2*pi*omega / period);
subplot(2, 1, 1);
plot(omega, y, 'b-');
title('Signal', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
[counts binValues] = hist(y, 64);
subplot(2, 1, 2);
bar(binValues, counts);
title('Histogram', 'FontSize', fontSize);
grid on;
Run my code above and you'll see your image.
  2 Comments
Zach
Zach on 29 Aug 2012
Hi Image Analyst,
Thank you for the response, this is very close to what I want. However, I am really looking to plot the pdf, is there a way to simply convert this hist() to a pdf? Thanks.
Image Analyst
Image Analyst on 30 Aug 2012
Simply divide the counts by the sum of the counts so that the total "area" equals 1.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!