1

I am getting the matrix dimension error for the following code. What I am trying to do below is modulation of an audio waveform but I could not get passed the error I specified. I have checked the length of the sample.wav and adjusted time axis(t) accordingly but I must have done something wrong. I appreciate if anyone could help. Thanks in advance!.

 function [sm]= modulation(ss,fc,mtype)
ss= audioread('C:\Users\furka\Documents\MATLAB\sample.wav');     %audio waveform to be modulated is loaded.
plot(ss)
length(ss)
t=linspace(0,3e6,3161538);
fc= input('Carrier Frequency=');            %carrier frequency will be determined by the user


mtype= menu('Modulation type?','dsb','dsbsc','ssb','fm');           %modulation type will be determined by the user
%fs=44100;                                   %sampling frequency is determined for common audio waveform.(44.1kHz)
%t= 0:1/fs:(2e-5)-1/fs;

if mtype==1 
    ka= 0.7;
    sm= ss.*(1+ka*cos(2*pi*fc*t));
    plot(t,sm)

    elseif mtype==2                         %if doublesideband suppress carrier is selected the statements below will be carried out.

    y = ss.*cos(2*pi*fc*t);
    plot(y)
    % sm = fftshift(fft(abs(y)));
  %  frequency_axis= (-fs/2):(fs/length(sm)):(fs/2-fs/length(sm));
    %plot(frequency_axis,sm)

    elseif mtype==3
    sm=0.5*[ss.*cos(2*pi*fc*t)-hilbert(ss).*sin(2*pi*fc*t)];
    plot(t,sm)

    elseif mtype==4
     kf=0.7;            %frequency sensitivity.
    sm= cos(2*pi*fc*t+2*pi*kf*int(ss,t,0,t));
    plot(t,sm)
end
end
Furkan Ülger
  • 13
  • 1
  • 6
  • Can you share `sample.wav` or use a wav file that we all have? Also what is the exact error and where does it occur? – Matt Apr 26 '18 at 18:32
  • Here is the sample.wav:https://ufile.io/m5mtu .The error occurs during every modulation statement so that the dimensions of t and sample do not match. – Furkan Ülger Apr 26 '18 at 18:37
  • I have uploaded the code itself as well . https://ufile.io/ht0zb The exact error is : Error using .* Matrix dimensions must agree. Error in Assgn_Modulation (line 16) sm= ss.*(1+ka*cos(2*pi*fc*t)); Error using .* Matrix dimensions must agree. Error in Assgn_Modulation (line 21) y = ss.*cos(2*pi*fc*t); Error using .* Matrix dimensions must agree. Error in Assgn_Modulation (line 28) sm=0.5*[ss.*cos(2*pi*fc*t)-hilbert(ss).*sin(2*pi*fc*t)]; I guess all of them are caused by the same mistake. – Furkan Ülger Apr 26 '18 at 18:44
  • I would say you need to use both outputs from `audioread`. The first is the sound the second is the frequency. `[ss, Fs] = audioread(...)` then length of sound `total = length(ss) / Fs`. Then `t = linspace(0, total, length(ss));` – Matt Apr 26 '18 at 18:44
  • I am glad for your help but still getting the same error , I have edited my code the way you expressed. `function [sm]= modulation(ss,fc,mtype) [ss,Fs]= audioread('C:\Users\furka\Documents\MATLAB\sample.wav'); %audio waveform to be modulated is loaded. Fs %sampling frequency is 44.1kHz for audio waveform. total=length(ss)/ Fs t=linspace(0,total,length(ss)); fc= input('Carrier Frequency='); %carrier frequency will be determined by the user`. – Furkan Ülger Apr 26 '18 at 19:06

1 Answers1

0

I can't access the link provided for the sound file but a time length of 3e6 seconds seems like a really long sound. I figure that's something around 30 days long.

Using the handel.mat example provided by MathWorks I get this.

load handel.mat
audiowrite('sample.wav', y, Fs);
[ss, Fs] = audioread('sample.wav');

t = linspace(0, length(ss) / Fs, length(ss));
t = t'; % this is important to match ss <-- this is your matrix dimension error I think
fc = 2000;
ka = 0.7
sm = ss .* (1 + ka * cos(2 * pi * fc * t));

The error of mismatched dimensions comes from linspace output being a row vector and audioread output being a column vector. Transpose one or the other. In my case: t = t'

Matt
  • 2,554
  • 2
  • 24
  • 45
  • After taking transpose , modulation worked just fine . Thanks very much . You have been very helpful. By the way the sound file is solely 1:11 minute long. I have taken 3e6 due to the sketched waveform's x axis `plot(ss)`. Uploaded to another source https://vocaroo.com/i/s1ClXAtgG9j1 – Furkan Ülger Apr 26 '18 at 19:19