Clear Filters
Clear Filters


This question is closed. Reopen it to edit or answer.

How should I generate a rectone from the following Matlab code

1 view (last 30 days)
function S=recttone(F,FS,LEN,PHASE)
frequency F, sampling frequency FS, Duration LEN in [sec]. PHASE is optional(phase shift). we can also use the following data.
Walter Roberson
Walter Roberson on 25 Jan 2017
Edited: Walter Roberson on 25 Jan 2017
How would you generate a single period of a rectangle tone?
Jan on 25 Jan 2017
@Syed Izzatullah: There are no "following data" in your question.

Answers (2)

Soumya Saxena
Soumya Saxena on 27 Jan 2017
Edited: Soumya Saxena on 27 Jan 2017
To generate rectangular pulse, you may use the "rectangularPulse" function in symbolic Math Toolbox.
The following documentation has more details:
Rectangular waveforms can also be created using the " phased.RectangularWaveform" function in Phased Array System Toolbox.
In general, to create periodic pulse you can see the examples given here using the Signal Processing Toolbox:

Image Analyst
Image Analyst on 27 Jan 2017
Edited: Image Analyst on 27 Jan 2017
I don't know what rectone is. Looks like you're trying to call it recursively. If you just need to make a tone in a rectangular pulse and save it to a wav file, and play it, try this:
% Program to create a wave file with constant amplitude and pitch.
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create the filename where we will save the waveform.
folder = pwd;
baseFileName = 'Test_Wave.wav';
fullFileName = fullfile(folder, baseFileName);
fprintf('Full File Name = %s\n', fullFileName);
% Set up the time axis:
Fs = 12000;
duration = 1; % seconds.
t = 1 : duration * Fs; % 2 seconds
% Set up the period (pitch, frequency):
T = 13; % Constant pitch if you use this.
% T = linspace(25, 8, length(t)); % Pitch changes if you use this.
% Create the maximum amplitude:
Amplitude = 32767 * ones(1, length(t));
% Zero out the first and last third to make a rectangular pulse
third = round(length(t) / 3);
Amplitude(1:third) = 0;
Amplitude(end-third:end) = 0;
% Construct the waveform:
y = int16(Amplitude .* sin(2.*pi.*t./T));
% y = abs(int16(Amplitude .* sin(2.*pi.*x./T)));
% Plot the waveform:
plot(t, y, 'b-');
title('Waveform', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fprintf('Writing file %s...\n', fullFileName);
% Write the waveform to a file:
audiowrite(fullFileName, y, Fs);
% Play the sound as many times as the user wants.
playAgain = true;
counter = 1;
while playAgain
% Play the sound that we just created.
fprintf('Playing file %s %d times...\n', fullFileName, counter);
player = audioplayer(y, Fs);
% Ask user if they want to play the sound again.
promptMessage = sprintf('You have played the sound %d times.\nDo you want to play the sound again?', counter);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(button, 'No')
playAgain = false;
counter = counter + 1;
% Alert user that we are done.
message = sprintf('Done playing %s.\n', fullFileName);
fprintf('%s\n', message);
promptMessage = sprintf('Done playing %s.\nClick OK to close the window\nor Cancel to leave it up.', fullFileName);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'OK')
close all; % Close down the figure.

This question is closed.


No tags entered yet.

Community Treasure Hunt

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

Start Hunting!