## Developer ZoneAdvanced Software Development with MATLAB

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

# Leveraging cloud capabilities from MATLAB5

Posted by Arvind Hosagrahara,

My day job involves using MATLAB analytics at the heart of big, powerful solution stacks both on the Cloud and running on-premise. The beauty of modern MATLAB as a part of the solution stack is that it offers an unparalleled environment to quickly develop and leverage various technologies to build complete solutions.

From an architecture perspective, the unique value proposition that MATLAB offers is that its extensibility-by-design enables me to choose the best-in-class technology products and services to construct my solution.

Why MATLAB?

Let us ask MATLAB that question and exercise the well known function that provides succinct answers to nearly any question.

why
% ans =
%
%    'Some tall and rich system manager knew it was a good idea.'


That funny bit of MATLAB functionality has been around since Cleve's MATLAB many decades ago (atleast as far back as 1987). In our current release (R2017b), the function will return an answer when requested much like the early versions. To replicate the trivial experiment, try typing why at your MATLAB command prompt.

We are now in 2018. We live in the age of the cloud. Conversational interfaces are appearing on many devices powered by cloud based services. It is a wonderful time to be alive when a personal assistant on your phone can tell you the time, give you directions, order your supplies and save you a trip to the grocery store.

MathWorks products have always played a critical role in accelerating the pace of discovery in science and technology. From prototyping signal processing algorithms all the way to embedding the functionality in a final product, the MathWorks tools play a vital role in a product lifecycle from development all the way to production - Cloud based services are no exception.

I have written about exposing MATLAB functionality as a service in the past but in this space, a demo is worth a thousand words, so to illustrate the point and answer the question, let us build our own cloud powered assistant - one that provides succinct answers to everything.

Building a service

As a first step, I will take the *why.m* file and modify it to provide a textual answer and expose it as a service on MATLAB Production Server running on the Amazon Web Services (AWS) public cloud. Go ahead and give it a try by clicking the button below.

With a click of the button, you have exercised MATLAB code running on MATLAB Production Server. While amusing, it does not fully answer the question on why we would consider using MATLAB for this. To answer that, we look at the MATLAB community and realize the fact that millions of MATLAB users don't all speak English, do they? But on the other hand, I don't speak chinese, japanese, spanish, french, german or any of the many languages spoken by our users worldwide.

However, I do have the power of MATLAB at my fingertips and the power of cloud powered cognitive services at my disposal and that should be all I need.

function str = translatedemo()
% TRANSLATEDEMO Example to illustrate calling a cloud based webservice.
% Call the Microsoft Translation service running on Azure

%% Configure the credentials and initialize
authKey = "059[REDACTED]c2f";
param.from = "en";

toArray = {"en","ja","es", "zh-Hans", "fr", "de"};
langChoice = ceil(rand(1,1)*6);
param.to = toArray{langChoice};

param.text = why;
queryParams = matlab.net.QueryParameter(param);

% Generate an answer
textStr = why;

% Get the text from our earlier example and translate it if required
if langChoice~=1

translateService = matlab.net.URI('https://api.microsofttranslator.com/v2/Http.svc/Translate', queryParams);

%% Translate the output of our text generator
% Create a HTTP request
type = matlab.net.http.MediaType('application/xml');
authField = matlab.net.http.field.GenericField('Ocp-Apim-Subscription-Key');
authField.Value = authKey;
acceptField = matlab.net.http.field.AcceptField(type);
translateRequest = matlab.net.http.RequestMessage(matlab.net.http.RequestMethod.GET, [authField, acceptField]);

% Send the request and parse the response
translateResponse = translateRequest.send(translateService);
str = char(translateResponse.Body.Data.getElementsByTagName('string').item(0).getTextContent());
else
% There is no need to translate english
str = textStr;
end



With a few lines of code, I call a translation service from Microsoft running on their Azure Cloud Computing Platform and get a textual translation of my answer. A few more lines of logic allows me to randomize the output language for this demo. We have made our MATLAB code multi-lingual. Go ahead, and try it out - if the results are in a language you don't understand, please try it again as I randomize the outputs.

Speak to me

Cloud powered services offer the capability to drive conversational interfaces. To further illustrate my point, I will use a Text-to-speech (TTS) technology from the same technology stack that powers Alexa. This is a service called Amazon Polly offered by the Amazon Web Services. With another simple API call, I can convert my output into a natural spoken voice in a multitude of languages and genders. Clicking the next button does exactly that (please check your headphone / speakers / system volume first).

MATLAB is your ally on the journey to the cloud

If you were clicking the buttons above, you have leveraged the power of two prominent public cloud services all from within a few lines MATLAB code that is also running on the cloud. MATLAB offers more than just prepackaged algorithms and APIs to do this. It is a platform optimized to enable you to develop and express your own algorithms.

To illustrate that point, answer our "why" question and complete our demo, I will integrate all of these cloud APIs with some audio signal processing. My tool of choice for this is the Audio System Toolbox™ which provides algorithms and tools for the design, simulation, and desktop prototyping of audio processing systems. It enables low-latency signal streaming from and to audio interfaces, interactive parameter tuning, and automatic generation of audio plugins for digital audio workstations. My objective is to make my cloud based system sound a bit more distinctive by adding some reverberation through some simple signal processing. This takes no more than a few lines of MATLAB code. Please give it a try to test it out and see if you can spot the difference:

% Setup a reverberator.
reverb = reverberator('PreDelay', 0, 'WetDryMix', 0.8,'SampleRate', 22050);
output = reverb(signal);



To conclude, MATLAB makes it simple to integrate the best-in-class algorithm designs with the variety of services to build full solutions. The demonstration polyglot talking bot operates right from within our blog post and illustrates a trivial example of building a cloud based voice powered service that provides an answer to nearly everything, tuned with some reverberation to sound the way we want it to.

The same principles permit me to build powerful real-world applications that harness the power of MATLAB signal/video processing as well as analytics in real-world industrial, IoT, automotive, aerospace, financial applications.

The takeaway that I am trying to articulate here is that if your journey is headed to the cloud, MATLAB is your trusted friend and ally with technology and powerful time-tested workflows to support it.

P.S: I shall probably be turning off this demo in a few weeks to save operational cost.

Get the MATLAB code

Published with MATLAB® R2017b

## 5 CommentsOldest to Newest

Michael replied on : 2 of 5
Last week I tried to (while usually we provide our users Tools buildt on MATLAB and deployed with MATLAB Compiler ) compile Matlab Code with Matlab Compiler SDK as .NET dll and embedded it into a website buildt on .NET. Thus I could probably give my users the possibility to leverage cloud powers without MATLAB Production Server. With 500+ Users I don't know if MATLAB Production Server is really feasible solution (I would need 50 licenses? ). On the other hand - I spent now quite some time on cloud technology and I'm a MATLAB guy - I guess that it's easier to hire somebody with Cloud experience and. NET/JAVA knowhow than finding someone who is deep into MATLAB and Cloud technology. Of course the intention of your article is to show that there is no need for special cloud know how since its already in MATLAB. However to build a real web based application in the cloud I think it takes something more. What is your opinion on my thoughts?
Arvind Hosagrahara replied on : 3 of 5
Michael, you bring up some great points. Thanks for commenting. First, let me clarify that the license on MATLAB Production Server does not limit the number of users. This is a common misconception so let me try to illustrate this. A single license of the MATLAB Production Server buys you a *concurrency* of 24 workers. This does NOT mean 24 users. It means that in the unlikely event that 24 users submitted a request at exactly the same time, and if the processing takes 1 second, then all 24 users would be receive their results in 1 second. The 25th user will ALSO get his answer but after 2 seconds and will wait on a queue for his turn, so will the 48th user, etc. The two ramifications of this that (a) the license buys capacity for concurrency (b) the quality of service can be made to scale or degrade gracefully with proper balancing of the load. Further this single license of 24 workers can be split up in any way you see fit to enable users to leverage it across different environments (eg: 16 for production, 4 for test and 4 for development). Your second point is one that is very close to my heart. You correctly identify my intention in writing this post. It was to assure MATLAB users that their cloud based workflows can be very close to the workflows that they already have invested the time to learn and use. The cloud offers many options but that does not necessarily translate to additional complexity for a typical MATLAB user. With the right infrastructure stack then those time-established workflows can translate to powerful real-world applications with vastly accelerated development of MATLAB powered solutions. That last sentence is a bold claim but I can back it up with an example in a future post. You are also correct in noting that you can achieve the same this via a .NET dll embedded in a website built on .NET. We strive to support different workflows for different use-cases. In doing so, depending on your application, you will need to handle queuing of multiple requests, starting, stopping and recovering from race conditions and a few more book-keeping tasks relating to scaling. Some of these are not difficult to do and some are, but all of them will require .NET / Java or other skills. It then becomes a question of NRE (Non-recurring engineering effort) to build such a system. The effort / time translates to resources and cost which in turn makes MATLAB Production Server appealing to many as it handles most of that out-of-the-box.
Michael replied on : 4 of 5
Thank you for your clarification. In my case I would have tried to shift a whole application to the cloud- meaning the application has to hold certain data while executing. I guess in such a case a worker would still be blocked ( let's take a simple example- a gui with two edit fields for numbers, a button and a result textfield for showing result - a simple calculator that adds numbers from the editfields by clicking the button). I guess it depends on implementation. Either the worker stores a new entered value in a e.g. AWS S3 container and then "shuts off". Then no worker would be blocked during idle time of the user. Or the application itself holds the data then I guess worker would be blocked also in idle time. ... I guess I have to read the docs. But you already made it quite clearer. The "queuing" of tasks sounds rather ugly and I hope I can avoid it. ;) I fully agree that in the end its cheaper to just buy something instead of rebuilding something that you are not good at. However - and I guess some of you can share this experience. As tool developers you are in mangers view in the end only something that accelerates the product, But not the product itself and therefore its rather unlikely that money is spent there. Thank you for sharing your thoughts. It was really enlightening.