# How to generate a raised-cosine window with a specific roll-off factor in Matlab?

I have recently started working on signal processing. For a small project, I have to shape cosine signals using a raised-cosine window with certain roll-off factors in Matlab.

To give you an example, I have the following code:

% Generate signals
Fs = 48000;
t = 0:1/Fs:1-1/Fs;
c = cos(2*pi*100*t);
h = hann(numel(c))';
windowed = c .* h;

% Plotting
subplot(3,1,1);
plot(t, c);
title("100 Hz cosine wave");

subplot(3,1,2);
plot(t, h);
title("Hann window");

subplot(3,1,3);
plot(t, windowed);
title("Windowed cosine wave");


The code generates the following plot:

In the end, I want to achieve something similar as in the third plot above. However, as you can see, the hann window rises relatively slowly. For my project, I need a window that has steeper slopes. The result should look as if I had performed the exact same multiplication as in the code above, but with a raised-cosine window with a roll-off factor of, e.g., 0.25. See the green plot in this image for an example (imagine that the amplitude at position 0 had the value 1). $$\beta$$ represents the roll-off factor in this case.

I have already tried several Matlab functions/objects like rcosdesign and comm.RaisedCosineTransmitFilter. I also looked into the mathematical definition of the raised-cosine window to generate it manually, but I could not make it work yet.

Could somebody give me a hint on how to generate an appropriate window with a specific roll-off factor (e.g. 0.25) to get a shaped signal similar to the one in the plot above? Any help would be highly appreciated.

Thanks.

• When you tried to use the in-built Matlab functions, what issues did you encounter? Commented Jun 5, 2022 at 0:55
• @Engineer The problem is that I don't know if these functions can be used to achieve what I need. The examples in the documentation show how to upsample and shape binary messages. However, in this case, I don't work with binary messages and I also do not need to upsample them. My goal is to use a raised-cosine window to attenuate the amplitudes of the time-domain representation of a cosine wave at the beginning and at the end of the signal. I updated my post to hopefully make things clearer.
– F105
Commented Jun 5, 2022 at 15:30

You want to generate a raised-cosine window to do, x .* w, instead of upsampling/filtering as is shown in the Matlab documentation. The trick to know about rcosdesign is how to chose the parameters, namely the samples-per-symbol and span. The product of the two must always be even, and the length of the resulting filter will be the product plus one. The span specifies how many symbols does the filter reach, set this to one. The samples-per-symbol specifies how densely sampled the window is, you'll need to chose an even integer for this. The resulting window will be an odd length so I'd suggest simply throwing away the last sample for example.

If the above doesn't satisfy you (it is a little imprecise forcing the tool to work), then I'd suggest writing your own just applying the formula: https://en.wikipedia.org/wiki/Raised-cosine_filter.

@F105 I'm not sure what you mean by "to make the sides of the window steeper", but MATLAB's 'rcosdesign()' command should do what you want. Perhaps experimenting with the following code will be of some help to you:

clear, clc % [Lyons, October 2018]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Design a raised cosine filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rolloff_factor = 0.2;
Num_of_Symbols = 6;
Samples_per_symbol = 8;

h = rcosdesign(Rolloff_factor, Num_of_Symbols, ...
Samples_per_symbol, 'normal');

Freq_Resp_Magnitude = abs(fft(h, 256));
figure(1), clf
subplot(2,1,1)
plot(h, ':bs', 'markersize', 4)
title('h filter imp. resp.'), xlabel('Samples'), grid on
subplot(2,1,2)
plot((0:127)/256, Freq_Resp_Magnitude(1:128), '-r'),
title('Filter freq. mag. resp.')
xlabel('Freq (times Fs)'), grid on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate a binary "message" and interpolate (upsample)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Binary_Message = 2*randi([0 1], 1, 15) - 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Upsample "Binary_Message_ by 'Samples_per_symbol'
% using zero stuffing interpolation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
U = Samples_per_symbol;  % Upsample factor = 'Samples_per_symbol'
Upsampled_Message = zeros(size(1:U*length(Binary_Message)));
Upsampled_Message([1:U:length(Upsampled_Message)]) = Binary_Message;
Upsampled_Message = Upsampled_Message(1:length(Upsampled_Message)...
-U+1); % Truncate trailing zeros

figure(2), clf
subplot(2,1,1)
plot(Binary_Message, ':bs', 'markersize', 4)
title('Binary Message'), grid on
subplot(2,1,2)
plot(Upsampled_Message, ':rs', 'markersize', 4),
title('Upsampled Message'), grid on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pass 'Upsampled_Message" thru the raised cos filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Filter_Out = conv(h, Upsampled_Message);

figure(3), clf
plot(Filter_Out, ':ro', 'markersize', 4)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the nonzero 'Upsampled_Message' samples
% on top of the filter's output sequence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on
Temp = [zeros(1,(length(h)-1)/2), Upsampled_Message];
Index = find(Temp==0);
Temp(Index) = nan;
plot([0.48*Temp], ':bs', 'MarkerFaceColor','b',...
'MarkerEdgeColor','b', 'MarkerSize',4)
hold off
title('Red = Filter Out,  Blue = Message'),
xlabel('Samples'), grid on

• Thank you for your answer. I updated my post to hopefully make things clearer. I performed some experiments with your code. You use rcosdesign to upsample and shape a binary message. Can you also generate a window with this function that can be used to shape a cosine wave, similar to the plot in my original post?
– F105
Commented Jun 5, 2022 at 18:45
• @F105 If I understand your question correctly, I believe the answer is no. Commented Jun 7, 2022 at 7:47