We live in a world of sensors. They're in robots and factories in our cars on our
wrists even in our refrigerators making sure our food stays fresh
these sensors generate signals in this video we'll use simulate to process a
signal from a sensor will perform spectral analysis to explore the signal
based on that we'll design and build digital filters as part of a single
processing algorithm we'll assess the performance of our algorithm and once
it's ready convert our model into C code that can be embedded into real-time
hardware so let's get started at the mathworks headquarters in Natick
Massachusetts there are solar panels that generate electrical power
we'll use measurements of the power produced by the array every 15 minutes
this is our signal of course the power depends on the amount of sunshine which
depends on the time of day and the weather predicting and managing the
variable production and demand is an important part of renewable energy
generation to smooth out the generated power on sunny days we can store some of
the power in a battery then on cloudy days we use it to supplement the lower
power generation let's design a system that can predict if it's going to be
sunny or cloudy using signal processing techniques in Simulink you start
Simulink by clicking the Simulink button on the MATLAB tool strip this opens the
start page where you can create new models find examples and even find basic
training we are starting our model from scratch so we'll choose blank model and
save it as sunny versus cloudy Simulink models are built up from blocks and
signal lines open the library browser to see all the blocks available we'll start
by visualizing two power signals one from a sunny day and one from a cloudy
day to view our signals let's drag two input ports or imports
for short into our model then add a scope block let's label the two in Port
blocks sunny day and cloudy day and the scope block time domain we can connect
the blocks together with signal lines by clicking and dragging to label the
signal line double click and type the name
now we need the data in MATLAB we have two vectors sunny day and cloudy day
representing the power measurements for two particular days in June the
corresponding time stamps are in tea day we also have the sample frequency in
samples per day in the variable FS sampling every 15 minutes means that we
get 96 samples per day we have to set the sample time of the two imports so
double click on each import to adjust its block parameters under the signal
attributes tab set the sample time to the reciprocal of the sampling frequency
to bring the data into Simulink we can go to the model settings window then the
data import export pane and add the time and 2 power signals as inputs we should
also set the total simulation time to one day we can now run our model by
clicking on the Run button in the tool strip let's double click on a scope
block to see the signals let's connect our data points using lines the smooth
yellow line is the sunny day the blue line shows that the cloudy day produces
less power as you'd expect and also has many short-term variations as the clouds
pass over the solar array so how can we use these features to decide if we have
a sunny day or a cloudy day well let's also look at these signals in
the frequency domain we'll use spectral analysis which helps
us measure the frequency content of each of the signals we first add a spectrum
analyzer block from the DSP system toolbox and then connect the two signals
to it to branch a signal line you can right click as you drag the signal line
to a block we have short signals so we'll need to change a few settings in
the spectrum analyzer block so we can see them properly this time we can run
our model from within the spectrum analyzer by pressing the green button at
the top let's turn on the legend to see which day is which so what are we
looking at x-axis is the frequency value the y-axis
shows us how much power is in our signal at a given frequency although the
default time unit is seconds we're actually measuring time in days so the
x-axis is cycles per day not cycles per second or Hertz we see that the low
frequency content is about the same for both days but the cloudy one has way
more high frequency content remember those short time variations well that's
how they look in the frequency domain so maybe we can tell Sonny from cloudy by
comparing the power and the higher frequencies to some threshold
however the panels produce more power in the summer and less in the winter which
means the threshold would have to change throughout the year well we'd like to
have a constant threshold for all seasons of the year if we normalize the
high frequency power by the power and the lower frequencies we can use a fixed
threshold so let's try computing the ratio of the total power in the top 75%
of frequencies to the total power in the bottom 25% of frequencies so how do we
separate the low and high frequencies by building digital filters we need a
low-pass filter for the low frequency content and a high-pass filter for the
high frequency content to build these two filters we first strip our model
back to a single import and scope block and drag in a filter realization wizard
block from the DSP system toolbox double-click the block to open the
filter designer app if you've designed filters in MATLAB you may have used this
app before let's click on the design filter icon to start designing the
low-pass filter because we only get samples every 15 minutes
we need a filter that can work with just a few samples will use a chebyshev type
1 IIR filter and will set the filter order to 4 we then have to specify a
cut-off frequency remember we wanted to let through the lower 25% of the
frequencies so we'll select normalized frequency
and set w pass to 0.25 and we'll keep the passband ripple to less than 0.05 DB
finally push the design filter button at the bottom we see that the new response
is what we want we can examine the delay introduced by our filter by clicking the
group delay response button for frequencies less than 0.25 the delay is
about 3 samples let's remember that for later now that we're done designing the
filter let's add it to our Simulink model by clicking the realize model icon
we'll call the block low-pass filter and select the option build model using
basic elements and that way we can see the basic Simulink blocks used to make
the filter like delay multiply and add blocks when we click the realize model
button a new subsystem is created in our model let's double click this to look
inside sure enough the filter is just unit
delays gains and add blocks which modern DSP chips and FPGAs are optimized to
implement now for the high frequency content well we can repeat the same
process to design and realize the high pass filter in this case the high pass
filter delays the input signal by one sample okay let's test our new filters
on the power signal we had three parallel paths one for the original
power signal one for the low pass filtered version and one for the high
pass filtered version we want to compare them but remember that the low pass
filter introduces a delay of three samples and the high pass filter
introduces a delay of one so we need to add some delay blocks to make sure that
three signals are aligned double-click on the model and type delay
to quickly add a delay block to the original signal of three samples and
another delay block to the high pass signal of two samples and we'll modify
the model to work with a much longer two-and-a-half-year data set that we
have which is stored in the variable power with time stamps and T on the
scope let's zoom in on six days the yellow line is the original power
our signal the blue line is the low-pass signal which represents the general
smooth trend and the red line is the high pass signal which captures the
variations due to cloud cover during cloudy periods these variations can be
large now that we've separated out the low and
high frequency signals we're ready to build our sunny test we need to
aggregate over enough time for our filters to give sensible results but we
don't want to wait too long to get an answer so we'll use a three hour window
which is just twelve measurements to store the filter outputs over a time
window we'll use a buffer block from the DSP
system toolbox we can set the buffer size to twelve to handle a three hour
window and we need a buffer on each filter path now we'll use some basic
math operation blocks to compute our sunny test like the absolute value sum
of elements and divide blocks let's add a block to compare the ratio with a
threshold with Simulink you can experiment easily to find a good value
for the threshold 22 works well but feel free to play with this value and see how
it changes the results the output of the comparison is zero for not sunny and one
for sunny let's scale it to match the maximum power just so it shows up nicely
on our plot and we should remove the extra input port on the scope block
before we run note that those buffers added some more delay to the signals so
to stay aligned with the original signal we'll need to increase that delay by 12
okay let's run and see how well our algorithm works if we zoom in on the
same days as before it looks like it's working pretty well but if we look at a
different set of six days we find a problem with our method sometimes it
says it's sunny in the middle of the night to solve this problem let's modify
our sunny test by adding a daylight detector
we'll simply compare the low frequency power to another threshold which lets us
know if the Sun is actually up we can combine the two conditions with an and
block rerun the model and check those days we see that we've
solved the false detection problem now that we have a working algorithm we want
to deploy it to an embedded system with embedded coder we can deploy to Arduino
Raspberry Pi zinc and many others let's deploy our model to an Arduino Uno board
we'll open the model settings window and select the hardware board then we
replace the scope block used for testing with an output port now let's generate
code this takes our Simulink model and generates all the C files that we need
to compile the model for deployment you can also generate generic C code for
other embedded systems or VHDL or Verilog code to deploy to an FPGA once
we have our generated code we poured it to the embedded hardware on our solar
array look at what we've done using Simulink we now have a steady source of
renewable energy you've seen how to go from a blank canvas to a signal
processing application running on an embedded system and you can try this too
the files are available for you to download now that you have a feel for
what working with Simulink is like it's time to learn it the best way to learn
Simulink is to work with it so startup Simulink on-ramp which will teach you
the basics it's free and takes just a couple of hours
welcome to Simulink