matlabsignal-processingmatlab-figure

Unable to understand product of Sinusoid signal with Hann window function in MATLAB R2024b


I'm writing a script for a certain signal processing application in MATLAB and have to multiply with the Hann window. Here's my code snippet:

Fpoints = 1e9;
d = 1e-7;
t = 0:1/Fpoints:d;
w1 = hann(16384);
...
figure(1);
plot(abs(fft(sin(2 * pi * 5e7 * t))));

figure(2);
plot(w1);

figure(3);
plot(w1 * sin(2 * pi * 5e7 * t));

I expected the sinewave to be distorted by the window from 0 to 16384 points. Instead it's plotting multiple different windows scaled by random values between +1 and -1 as seen in the image. What could be the reason?

This is the image.


Solution

  • Some observations:

    t = 0:1/Fpoints:d; % this results in an array 1x101

    While:

    w1 = hann(16384); % results in an array 16384x1

    Multiplying them together with * results in a matrix which is 16384x101. So here there is some room for improvement for sure.

    I cannot really tell however what you actually want from your code currently, but let's assume the following:

    Here's a demo for now, please tell me if this is not what you wanted to do?

    N = 16384;
    Fpoints = 1e9;
    t = (0:N-1)/Fpoints;
    w1 = hann(N)';
    signal = sin(2 * pi * 5e7 * t);
    windowedSignal = w1 .* signal; % w1 is a column, signal is row
    figure(1);
    subplot(3,1,1)
    plot(t, signal)
    ylabel("Signal")
    xlim([0, t(end)])
    subplot(3,1,2)
    plot(t, w1)
    xlim([0, t(end)])
    ylabel("Window")
    subplot(3,1,3)
    plot(t, windowedSignal)
    xlim([0, t(end)])
    ylabel("Multiplied")
    

    Demo


    Alternatively, if you just want to "pulse" the sine wave with the hanning window, you need to make the hanning window the same size as the signal by adding 0 at the end (or at the back if needed).

    Something like this:

    N = 100000;
    NHann = 16384;
    Fpoints = 1e9;
    t = (0:N-1)/Fpoints;
    w1 = hann(NHann);
    w1 = [w1; zeros(N-NHann,1)]';
    
    signal = sin(2 * pi * 5e7 * t);
    windowedSignal = w1 .* signal; % w1 is a column, signal is row
    
    figure(1);
    subplot(3,1,1)
    plot(t, signal)
    ylabel("Signal")
    xlim([0, t(end)])
    subplot(3,1,2)
    plot(t, w1)
    xlim([0, t(end)])
    ylabel("Window")
    subplot(3,1,3)
    plot(t, windowedSignal)
    xlim([0, t(end)])
    ylabel("Multiplied")
    

    Demo pulsed