# How can I write a Matlab code on Digital Signals Processing ?

6 vues (au cours des 30 derniers jours)
Jone Erikson le 23 Août 2020
Commenté : Rena Berman le 12 Oct 2020
How can I write a MATLAB function (not a script!) to generate a periodic waveform of total length L. Each period must be a pulse of amplitude A that lasts a total ofM samples followed by T−M samples that are zero so that the overall period is T. The result should be a squarewave. Could you please help with this code, with a brief explaination of the code
##### 2 commentairesAfficher AucuneMasquer Aucune
Stephen23 le 26 Août 2020
Modifié(e) : Stephen23 le 26 Août 2020
"How can I write a Matlab code on Digital Signals Processing ?"
How can I write a MATLAB function (not a script!) to generate a periodic waveform of total length L. Each period must be a pulse of amplitude A that lasts a total ofM samples followed by T−M samples that are zero so that the overall period is T. The result should be a squarewave. Could you please help with this code, with a brief explaination of the code
Rena Berman le 12 Oct 2020

Connectez-vous pour commenter.

### Réponse acceptée

Thiago Henrique Gomes Lobato le 23 Août 2020
This should work for you, the code is almost self explanatory:
L = 1024;
Periods = 4;
M = 128;
A = 1;
figure,plot( squareWave(L,M,Periods,A) )
function signal = squareWave(L,M,Periods,A)
signal = zeros(L,1); %initialize signal with zeros
if mod(L,Periods) ~= 0
signal = -1; % False input data
end
T = L/Periods; % Get length
% Replace only non-zero values
for idx=1:Periods
signal( 1+(idx-1)*T:1+(idx-1)*T+M) = A;
end
end
##### 2 commentairesAfficher AucuneMasquer Aucune
Thiago Henrique Gomes Lobato le 23 Août 2020
Which error do you become? Here it works fine. Remember that you need to save it to a file and run, and not just evaluate it (F9).
Stephen23 le 26 Août 2020
The code above is not running Thigao
>> squareWave
Error: File: squareWave.m Line: 14 Column: 30
Function definitions are not permitted in this context.
Line 14: signal(1+(idx-1)*T:1+(idx-1)*T+M)= A;
Also, why did you assume these values for: L, M, A, and Periods?

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Waveform Generation dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by