Making a .wav file in simulink

Discussion in 'Programmer's Corner' started by Jon_Snow, Mar 5, 2013.

  1. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    Earlier I wrote a matlab script to modulate and demodulate an am radio wave and make a .wav file. I did it with a matlab script and it worked alright.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. close all;
    4. clear all;
    5. fs = 2e6;
    6. t = [0:1/fs:5];
    7.  
    8. f_cut_off1 = 10000;
    9. f_cut_off2 = 10;
    10. f = 2000;                                        %input frequency
    11. fc = 940e3;                                     %carrier frequency
    12.  
    13. k = length(t);
    14.  
    15.    
    16. input_sig = (0.2)*cos(2 * pi * f * t);                %input signal
    17.  
    18. wavwrite(input_sig,fs,32,'input_sig');
    19.  
    20. carrier_sig = (0.5)*cos( 2 * pi * fc * t);            %carrier signal
    21.  
    22.  
    23.  
    24. mod_sig = input_sig .* carrier_sig + carrier_sig;  %modulated signal
    25.  
    26.  
    27.                                        %interval to randonize noise
    28. noise = rand(1, k) - .5;                    %random nosie
    29.  
    30. new_mod = mod_sig + noise;          % mod signal + noise
    31.  
    32.  
    33. %Apply HW Rectifier
    34.  
    35.  
    36. for i = 1:k
    37. if new_mod(i) <= 0
    38.     x(i) = 0;
    39. else
    40.     x(i) = new_mod(i);
    41. end
    42. end
    43.  
    44.  
    45.  
    46. rc1 = 1 / (2 * pi * f_cut_off1);
    47. rc2 =  1 / (2 * pi * f_cut_off2);
    48.  
    49. %Apply Low Pass Filter
    50. num2 = [0    1];
    51. den2 = [rc1   1];
    52. subplot(324), bode(num2, den2)
    53.  
    54. sys2 = tf(num2, den2);
    55.  
    56. lpf = lsim(sys2, x, t);
    57.  
    58.  
    59.  
    60.  
    61. %Apply High Pass Filter    
    62.  
    63. num1 = [rc2  0];
    64. den1 = [rc2 1];
    65.  
    66.  
    67. sys1 = tf(num1, den1);
    68.  
    69. hpf = lsim(sys1, lpf, t);
    70.  
    71. vout = hpf;
    72.  
    73. %Apply Convolution
    74. g = 20;
    75. h = (1 / g) * ones(1, g);
    76.  
    77. y = conv(h ,vout);
    78.  
    79.  
    80.  
    81. wavwrite(y,fs,32,'vout')
    82.  
    83.  
    Now I'm trying to do the same thing, but I'm calling simulink from a script. The problem I'm having is that I'm only getting a 48 byte wav file at the end of my script. In my first script my wav file was 40Mb. Is anyone here familiar with simulink? Here's my code. I can put my simulink model up if necessary.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. clear all
    4. clc
    5. fs = 2e6;
    6. t = [0:1/fs:5];
    7. t = t';
    8. f_cut_off1 = 10000;
    9. f_cut_off2 = 10;
    10. f = 2000;                                        %input frequency
    11. fc = 940e3;                                      %carrier frequency
    12.  
    13. %k = 1;%length(t);
    14.  
    15. rc1 = 1 / (2 * pi * f_cut_off1);
    16. rc2 =  1 / (2 * pi * f_cut_off2);
    17.  
    18. input_sig = 1*cos(2 * pi * f * t);                %input signal
    19. input_sig1 = [t'; input_sig'];
    20. save('input_sig1.mat', 'input_sig1');
    21.  
    22. carrier_sig = 1*cos( 2 * pi * fc * t);        %carrier signal
    23. carrier_sig1 = [t' ; carrier_sig'];
    24. save('carrier_sig1.mat', 'carrier_sig1');
    25.  
    26. load_system('am_mod_simulinkORIGINAL');
    27.  
    28. set_param('am_mod_simulinkORIGINAL','Stoptime',num2str(t(size(t,1),1)));
    29.  
    30. set_param('am_mod_simulinkORIGINAL/LPF', 'Numerator', '[0 1]');
    31. den1 = strcat('[',num2str(rc1),',','1',']')
    32. set_param('am_mod_simulinkORIGINAL/LPF', 'Denominator', den1);
    33. den2 = strcat('[',num2str(rc2),',','1',']')
    34. set_param('am_mod_simulinkORIGINAL/HPF', 'Denominator', den2);
    35. num2 = strcat('[',num2str(rc2),',','0',']')
    36. set_param('am_mod_simulinkORIGINAL/HPF', 'Numerator', num2);
    37.  
    38.  
    39. save_system('am_mod_simulinkORIGINAL');
    40.  
    41. sim('am_mod_simulinkORIGINAL');
    42.  
    43. p = load('outputsignal.mat');
    44.  
    45. data = p.ans;
    46.  
    47. g=20;
    48. h = (1 / g) * ones(1, g);
    49. y = conv(h ,data(2,:));
    50.  
    51. wavwrite('y',fs,32,'out_sig');
    52.  
    53.  
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Not sure, but you may need to set a simulation time for Simulink from your script.

    Is the 48 bytes relevant data?
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    tshuck likes this.
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So do the 4 bytes correlate with your expected results? There may not be enough data to determine that though...
     
  5. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    I got it to work. Instead of doing data = p.ans, I did data = ans. That seemed to do the trick.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    By that solution, making data = p, should also work...
     
Loading...